-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathapp.js
41 lines (30 loc) · 944 Bytes
/
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
const express = require("express");
const helmet = require("helmet");
const xss = require("xss-clean");
const compression = require("compression");
const cors = require("cors");
const routes = require("./routes");
const errorHandler = require("./middlewares/error-handler");
const NotFoundError = require("./utils/errors/notfound.error");
require("dotenv/config");
const app = express();
// set security HTTP headers
app.use(helmet());
// parse json request body
app.use(express.json());
// parse urlencoded request body
app.use(express.urlencoded({ extended: true }));
// sanitize request data
app.use(xss());
// gzip compression
app.use(compression());
// enable cors
app.use(cors());
app.options("*", cors());
app.use(routes);
// return a NotFoundError for any unknown api request
app.use((req, res, next) => {
next(new NotFoundError(`Cannot ${req.method} ${req.originalUrl}`));
});
app.use(errorHandler);
module.exports = app;