-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathapp.js
73 lines (59 loc) · 2.26 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
/* eslint-disable no-undef */
const express = require('express');
const path = require('path');
const cookieParser = require('cookie-parser');
const logger = require('morgan');
const rateLimit = require('express-rate-limit');
const favicon = require('serve-favicon');
const cors = require('cors');
const helmet = require('helmet');
const magic = require('express-routemagic');
const statusCode = require('./helpers/statuscode');
const app = express();
const port = process.env.PORT || 3000;
app.use(favicon(path.join(__dirname, 'favicon.ico')));
// eslint-disable-next-line no-unused-vars
app.use(logger('dev', { skip: (req, res) => process.env.NODE_ENV === 'test' }));
app.use(express.json());
app.use(express.urlencoded({ extended: false }));
app.use(cookieParser());
app.use(express.static(path.join(__dirname, 'public')));
// --| Enable CORS for GET method only
const corsOptions = {
origin: '*',
optionsSuccessStatus: statusCode.STATUS_OK,
methods: ['GET']
};
app.use(cors(corsOptions));
// --| Need this set for Heroku
app.set('trust proxy', 1);
// --| Enable Helmet to secure the API a bit
app.use(helmet());
// --| Limit the API for 100 requests per minute only
const APILimiter = rateLimit({
windowMs: 60000,
max: 100,
message: { message: 'Too many requests! Try again after 1 minute?' }
});
// --| Limit the API for 100 requests per minute only in /v1/ folder
app.use('/v1/', APILimiter);
// --| Automatic route everything in routes folder
magic.use(app, {
routesFolder: 'routes',
invokerPath: __dirname,
allowSameName: true
});
// --| 404 Response
// eslint-disable-next-line no-unused-vars
app.use((req, res, next) => res.status(statusCode.STATUS_NOT_FOUND).json({ message: 'Sorry, can\'t find the page you are looking for 👀' }));
// --| Error handler
// eslint-disable-next-line no-unused-vars
app.use((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 : { };
});
// --| Log the info on server startup
// eslint-disable-next-line no-console
app.listen(port, () => process.env.NODE_ENV !== 'test' ? console.log(`🖥️ Server runs and is listening on port \x1b[34m${port}\x1b[0m.`) : '');
module.exports = app;