-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
48 lines (38 loc) · 1.23 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
require('@babel/register')({
presets: [ '@babel/preset-env' ],
plugins: [ '@babel/plugin-transform-runtime' ]
});
var path = require('path');
var routes = require('./routes');
var createError = require('http-errors');
var express = require('express');
var httpLogger = require('morgan');
var log = require('./libs/log')(module);
var mongoose = require('./libs/mongoose');
var app = express();
/* basic modules */
app.use(httpLogger('dev'));
app.use(express.json());
app.use(express.static(path.join(__dirname, 'public')));
app.use(function(req, res, next) {
res.header('Access-Control-Allow-Origin', '*');
res.header('Access-Control-Allow-Methods', 'GET, PUT, POST, DELETE, OPTIONS');
res.header('Access-Control-Allow-Headers', 'Origin, X-Requested-With, Content-Type, Accept');
next();
});
/* imported modules */
mongoose.connect(process.env.NODE_ENV === 'test' ? 'mongodb://localhost:27017/texts-test' : 'mongodb://localhost:27017/texts');
routes.set(app);
/* errors handlers */
app.use(function(req, res, next) {
next(createError(404));
});
app.use(function(err, req, res, next) {
log.error(err);
if (err.code) {
res.status(err.code).send(err);
} else {
res.status(err.status || 500);
}
});
module.exports = app;