-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathapp.js
52 lines (39 loc) · 1.13 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
import express from "express";
import helmet from "helmet";
import xss from "xss-clean";
import cors from "cors";
import httpStatus from "http-status";
import config from "./config/config.js";
import morgan from "./config/morgan.js";
import routes from "./routes/index.js";
import { errorConverter, errorHandler } from "./middleware/error.js";
import ApiError from "./utils/ApiError.js";
const app = express();
if (config.env !== "test") {
app.use(morgan.successHandler);
app.use(morgan.errorHandler);
}
// 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 }));
// static file
app.use(express.static("uploads"));
// sanitize request data
app.use(xss());
// enable cors
app.use(cors());
app.options("*", cors());
// v1 api routes
app.use("/v1", routes);
// send back a 404 error for any unknown api request
app.use((req, res, next) => {
next(new ApiError(httpStatus.NOT_FOUND, "Not found"));
});
// convert error to ApiError, if needed
app.use(errorConverter);
// handle error
app.use(errorHandler);
export default app;