-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
57 lines (50 loc) · 1.65 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
`${
opts.esm
? `import createError from 'http-errors';
import express from 'express';
import cookieParser from 'cookie-parser';
import logger from 'morgan';
${opts.compression ? `import compression from 'compression';` : ``}
${opts.helmet ? `import helmet from 'helmet';` : ``}
import mainRouter from './routes/routes.js';
${opts.sequelize ? "import usersRouter from './routes/users.js';" : ''}
`
: `
const createError = require('http-errors');
const express = require('express');
const cookieParser = require('cookie-parser');
const logger = require('morgan');
${opts.compression ? `const compression = require('compression');` : ``}
${opts.helmet ? `const helmet = require('helmet');` : ``}
const mainRouter = require('./routes/routes');
${opts.sequelize ? "const usersRouter = require('./routes/users');" : ''}`
}
const app = express();
app.disable('x-powered-by');
app.use(logger('dev'));
app.use(express.json());
app.use(cookieParser());
${opts.compression ? `app.use(compression());` : ``}
${
opts.helmet
? `
// be aware of helmet configurations for you best use case https://helmetjs.github.io/
app.use(helmet());
`
: ''
}
app.get('/favicon.ico', (req, res) => res.status(204));
app.use('/', mainRouter);
${opts.sequelize ? "app.use('/users', usersRouter);" : ''}
// catch 404 and forward to error handler
app.use((req, res, next) => {
next(createError(404));
});
// error handler
app.use((err, req, res) => {
// only providing detailed error in development
res.status(err.status || 500);
return res.json( req.app.get('env') === 'development' ? err : { err: "Ops."} );
});
${opts.esm ? 'export default app;' : 'module.exports = app;'}
`;