-
Notifications
You must be signed in to change notification settings - Fork 59
/
app.js
79 lines (65 loc) · 2.15 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
const express = require('express');
const path = require('path');
const logger = require('morgan');
const cookieParser = require('cookie-parser');
const bodyParser = require('body-parser');
const Scheduler = require('./lib/scheduler');
const ScheduledJobs = require('./lib/scheduled-jobs');
Scheduler.runEvery('0.5 days', ScheduledJobs.cleanEbooks);
const AppErrors = require('./lib/app-errors');
const index = require('./routes/index');
const users = require('./routes/users');
const booksBeta = require('./routes/api/books-beta');
const booksV1 = require('./routes/api/books-v1');
const version = require('./routes/api/version');
const app = express();
// view engine setup
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'pug');
// uncomment after placing your favicon in /public
if (process.env.NODE_ENV !== 'test') {
app.use(logger('dev'));
} else {
Scheduler.clearIntervals();
}
app.use(bodyParser.json({ limit: '10mb' }));
app.use(bodyParser.urlencoded({ limit: '10mb', extended: false }));
app.use(cookieParser());
app.use(express.static(path.join(__dirname, 'public')));
app.use((err, req, res, next) => {
if (err) {
const apiError = AppErrors.getApiError(err);
AppErrors.respondWithError(res, apiError);
} else {
next();
}
});
const allowCrossDomain = (req, res, next) => {
res.header('Access-Control-Allow-Origin', '*');
res.header('Access-Control-Allow-Methods', 'GET,PUT,POST,DELETE');
res.header('Access-Control-Allow-Headers', 'Content-Type');
next();
};
app.use(allowCrossDomain);
app.use('/', index);
app.use('/users', users);
app.use('/api/books', booksBeta);
app.use('/api/v1/books', booksV1);
app.use('/api/version', version);
app.use('/api/v1/version', version);
// catch 404 and forward to error handler
app.use((req, res, next) => {
const err = new Error('Not Found');
err.status = 404;
next(err);
});
// error handlers
// no stacktraces leaked to user
app.use((err, req, res) => {
res.status(err.status || 500);
res.render('error', {
message: err.message,
error: app.get('env') === 'development' ? err : {},
});
});
module.exports = app;