-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
65 lines (53 loc) · 1.48 KB
/
app.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
const express = require('express')
const morgan = require('morgan')
const cookieParser = require('cookie-parser')
const bodyParser = require('body-parser')
const cors = require('cors')
const mongoose = require('mongoose')
const config = require('config')
const winston = require('winston')
const slackTransport = require('slack-winston').Slack
// logging
winston.level = config.logLevel
if (config.slackErrorLogging) {
winston.add(slackTransport, {
domain: 'sh8email',
token: process.env.SH8_SLACK_TOKEN,
webhook_url: process.env.SH8_SLACK_WEBHOOK_URL,
channel: 'sh8-server-error',
level: 'error',
})
}
winston.handleExceptions()
// routers
const mails = require('./routes/mails')
// database setup
mongoose.connect(config.dbUri)
// Use native promises
mongoose.Promise = global.Promise
const app = express()
if (config.useMorgan) {
app.use(morgan('dev'))
}
app.use(bodyParser.json())
app.use(bodyParser.urlencoded({ extended: false }))
app.use(cookieParser())
app.use(cors({ origin: config.corsOrigin }))
app.use('/api/', mails)
// error handler
app.use((err, req, res, next) => {
// set locals, only providing error in development
res.locals.message = err.message
res.locals.error = req.app.get('env') === 'development' ? err : {}
winston.error(err)
res.status(err.status || 500)
res.send({
message: err.message,
})
})
// catch 404
app.use((req, res, next) => {
winston.info(`Not Found: ${req.originalUrl}`)
res.sendStatus(404)
})
module.exports = app