-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
82 lines (65 loc) · 2.29 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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
const createError = require('http-errors');
const express = require('express');
const path = require('path');
const cookieParser = require('cookie-parser');
const logger = require('morgan');
const config = require('./config');
require('mongoose').connect(
`mongodb://${config.mongoDB.host}:${config.mongoDB.port}/${config.mongoDB.database}`, {
useNewUrlParser: true,
useUnifiedTopology: true
});
const auth = require('./middlewares/auth');
const indexRouter = require('./routes/index');
const uploadRouter = require('./routes/upload');
const registerRouter = require('./routes/register');
const tokenRouter = require('./routes/token');
const aboutRouter = require('./routes/about');
const helpRouter = require('./routes/help');
const searchRouter = require('./routes/search');
const addTermRouter = require('./routes/addTerm');
const modifyRouter = require('./routes/modify');
let app = express();
app.locals.moment = require('moment');
app.locals.config = config;
// view engine setup
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'ejs');
app.use(logger('dev'));
app.use(express.json());
app.use(express.urlencoded({extended: false}));
app.use(cookieParser());
app.use(express.static(path.join(__dirname, 'public')));
app.use(auth.tokenParser);
app.use('/', indexRouter);
app.use('/upload', uploadRouter);
app.use('/register.html', registerRouter);
app.use('/token.html', tokenRouter);
app.use('/about.html', aboutRouter);
app.use('/help.html', helpRouter);
app.use('/search', searchRouter);
app.use('/addTerm', addTermRouter);
app.use('/modify', modifyRouter);
// catch 404 and forward to error handler
app.use(function (req, res, next) {
next(createError(404));
});
// error handler
app.use(function (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 : {};
res.status(err.status || 500);
let contentType = req.headers['content-type'];
console.error(err);
if (contentType && contentType.indexOf('application/json') !== -1) {
return res.json({
msg: "Internal server error."
});
} else {
// render the error page
res.render('error');
}
next();
});
module.exports = app;