-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
79 lines (66 loc) · 2.17 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
74
75
76
77
78
79
import createError from "http-errors";
import express from "express";
import path from "path";
import cookieParser from "cookie-parser";
import logger from "morgan";
import winston from "winston";
import cors from "cors";
import fileUpload from "express-fileupload";
import * as commonResponse from "./helper/commonResponse.js";
import * as indexRouter from "./routes/index.js";
import { mongodb } from "./helper/index.js";
import { dirname } from "path";
import { fileURLToPath } from "url";
const __dirname = dirname(fileURLToPath(import.meta.url));
global.logger = winston.createLogger({
level: "info",
format: winston.format.combine(
winston.format.timestamp({ format: "YYYY-MM-DD HH:mm:ss" }),
winston.format.printf((info) => `${info.timestamp} [${info.level}] : ${info.message}`)
),
defaultMeta: { service: "user-service" },
transports: [
new winston.transports.File({
name: "file.info",
filename: "./logs/info.log",
level: "info",
maxsize: 1024 * 1024 * 1, // Bytes
maxFiles: 5,
}),
new winston.transports.File({
name: "file.error",
filename: "./logs/error.log",
level: "error",
maxsize: 1024 * 1024 * 1, // Bytes
maxFiles: 5,
}),
],
});
const app = express();
app.use(cors());
app.set("views", path.join(__dirname, "views"));
app.set("view engine", "ejs");
app.use(logger("dev"));
app.use(express.json());
app.use(express.urlencoded({ extended: false }));
app.use(fileUpload());
app.use(cookieParser());
app.use(express.static(path.join(__dirname, "public")));
indexRouter.initialize(app);
mongodb.mongo_connection();
app.use((req, res, next) => {
const error = new Error("NOT_FOUND");
error.status = 404;
next(error);
});
app.use((error, req, res, next) => {
res.status(error.status || 500);
return commonResponse.error(res, error.message, error.status);
});
app.use((err, req, res, next) => {
res.locals.message = err.message;
res.locals.error = req.app.get("env") === "development" ? err : {};
res.status(err.status || 500);
res.render("error");
});
export default app;