-
Notifications
You must be signed in to change notification settings - Fork 1
/
app.js
60 lines (48 loc) · 1.7 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
/* 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 fileUpload = require('express-fileupload');
const cors = require('cors');
const helmet = require('helmet');
const memesRoutes = require('./routes/v1/routes');
const port = process.env.PORT || 6969;
const app = express();
app.use(favicon(path.join(__dirname, 'favicon.ico')));
app.use(logger('dev'));
app.use(express.json());
app.use(express.urlencoded({ extended: true }));
app.use(cookieParser());
app.use(express.static(path.join(__dirname, 'public')));
// --| Enable CORS for all requests
app.use(cors());
// --| Enable Helmet to secure the API a bit
app.use(helmet());
// --| Need this set for Heroku
app.set('trust proxy', 1);
// --| Limit the API for 50 requests per IP every minute
const APILimiter = rateLimit({
windowMs: 60000,
max: 65,
skipFailedRequests: true,
message: {
status: 429,
message: 'Too many requests! Stop spamming 😡'
}
});
app.use(fileUpload({ createParentPath: true }));
app.use('/api/', APILimiter);
app.use('/api/v1', memesRoutes.router);
// --| 404 Response
// eslint-disable-next-line no-unused-vars
app.use((req, res, next) => res.status(404).json({
status: 404,
message: 'Sorry, can\'t access the endpoint you are looking for 👀. Is it POST or GET 🤔 ?'
}));
// --| Log the info on server startup
// eslint-disable-next-line no-console
app.listen(port, () => console.log(`🖥️ Server runs and is listening on port \x1b[34m${port}\x1b[0m.`));
module.exports = app;