-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
72 lines (64 loc) · 1.92 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
require('dotenv').config();
const createError = require('http-errors');
const express = require('express');
const path = require('path');
const cookieSession = require('cookie-session');
const logger = require('morgan');
const passport = require('passport');
const fileUpload = require('express-fileupload');
const i18n = require('i18n');
const HttpStatus = require('http-status-codes');
const routes = require('./routes');
const app = express();
/// router logging output
if (process.env.NODE_ENV !== 'test') {
app.use(logger('dev'));
}
/// multipart file upload support (when not uploading direct to S3)
app.use(
fileUpload({
useTempFiles: !process.env.AWS_S3_BUCKET,
})
);
/// configure allowed file upload types and max file size
app.use(express.raw({ type: ['image/*'], limit: '10mb' }));
/// support json content body
app.use(express.json());
/// support standard form urlencoded body
app.use(express.urlencoded({ extended: false }));
/// support forwarded headers from intermediate proxies
app.set('trust proxy', 1);
/// set up session handling in cookies
app.use(
cookieSession({
secret: process.env.SESSION_SECRET,
secure: process.env.NODE_ENV === 'production',
})
);
/// use passport for authentication
app.use(passport.initialize());
app.use(passport.session());
/// support internationalization of strings
i18n.configure({
locales: ['en'],
directory: path.join(__dirname, 'locales'),
});
app.use(i18n.init);
/// set up local variables commonly used in all requests
app.use((req, res, next) => {
/// set the current logged in user, if any
res.locals.currentUser = req.user;
next();
});
/// load in all the configured routes in /routes/index.js
app.use(routes);
/// catch 404 and forward to error handler
app.use((req, res, next) => {
next(createError(404));
});
/// error handler
app.use((err, req, res) => {
/// render the error
res.status(HttpStatus.INTERNAL_SERVER_ERROR).json(err);
});
module.exports = app;