-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.js
More file actions
61 lines (47 loc) · 1.67 KB
/
main.js
File metadata and controls
61 lines (47 loc) · 1.67 KB
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
const server = require('express')()
const bodyParser = require('body-parser')
const cookieParser = require('cookie-parser')
const mongoose = require('mongoose')
const proxy = require('http-proxy-middleware')
const morgan = require('morgan')
const exphbs = require('express-handlebars')
global.config = require('./config')
const mailer = require('./modules/mailer')
const authenticate = require('./middlewares/authenticate')
mailer.createTestInstance()
mongoose.Promise = global.Promise
mongoose.connect('mongodb://localhost/demo', {
useMongoClient: true,
}).then((mongoConnection) => {
global.mongoConnection = mongoConnection
// Logging
server.use(morgan('METHOD: :method, URL: :url, STATUS: :status, TIME: :response-time ms'))
// Body parser
server.use(bodyParser.urlencoded({ extended: true }))
server.use(bodyParser.json())
// Cookie parser
server.use(cookieParser())
// Enable handlebars view engine
const hbs = exphbs.create({})
server.engine('handlebars', hbs.engine, { defaultLayout: 'base' })
server.set('view engine', 'handlebars')
server.use((error, req, res, next) => {
if (error.type === 'entity.parse.failed') return res.status(422).json({ message: 'Invalid JSON provided' })
return next()
})
// Proxy
server.use('/u', authenticate, proxy({
target: 'http://localhost:4000',
changeOrigin: true,
onProxyReq(proxyReq, req) {
proxyReq.setHeader('x-access-identity-email', req.decodedToken.email)
},
}))
// Controllers
server.use(require('./controllers'))
// 404
server.all('*', (req, res) => res.status(404).send('Not found'))
// Port
server.listen(global.config.port || 3000)
})
.catch(e => console.error(e))