-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
executable file
·74 lines (66 loc) · 1.75 KB
/
server.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
require('dotenv-safe').config()
const express = require('express')
const bodyParser = require('body-parser')
const morgan = require('morgan')
const compression = require('compression')
const helmet = require('helmet')
const cors = require('cors')
const passport = require('passport')
const app = express()
const i18n = require('i18n')
const initMongo = require('./config/mongo')
const path = require('path')
// Setup express server port fron ENV, default: 3000
app.set('port', process.env.PORT || 3000)
// Enable only in develoment HTTP request logger middleware
if (process.env.NODE_ENV === 'development') {
app.use(morgan('dev'))
}
// Redis cache enabled by env variable
if (process.env.USE_REDIS === 'true') {
const getExpeditiousCache = require('express-expeditious')
const cache = getExpeditiousCache({
namespace: 'expresscache',
defaultTtl: '1 minute',
engine: require('expeditious-engine-redis')({
host: process.env.REDIS_HOST,
port: process.env.REDIS_PORT
})
})
app.use(cache)
}
// for parsing json
app.use(
bodyParser.json({
limit: '20mb'
})
)
// for parsing application/x-www-form-urlencoded
app.use(
bodyParser.urlencoded({
limit: '20mb',
extended: true
})
)
// i18n
i18n.configure({
locales: ['en', 'es'],
directory: `${__dirname}/locales`,
defaultLocale: 'en',
objectNotation: true
})
app.use(i18n.init)
// Init all other stuff
app.use(cors())
app.use(passport.initialize())
app.use(compression())
app.use(helmet())
app.use(express.static('public'))
app.set('views', path.join(__dirname, 'views'))
app.engine('html', require('ejs').renderFile)
app.set('view engine', 'html')
app.use(require('./app/routes'))
app.listen(app.get('port'))
// Init MongoDB
initMongo()
module.exports = app // for testing