-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
91 lines (83 loc) · 2.01 KB
/
server.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
80
81
82
83
84
85
86
87
88
89
90
91
"use strict";
require("dotenv").config();
require("pretty-error").start();
const express = require("express");
const app = express();
const PORT = process.env.PORT || 3000;
const morgan = require("morgan");
const cors = require("cors");
const compression = require("compression");
const hpp = require("hpp");
const helmet = require("helmet");
const log4js = require("log4js");
const paginate = require("express-paginate");
const dayjs = require("dayjs");
const { errorHandler } = require("./middleware/errorHandler");
const log = log4js.getLogger("entrypoint");
log.level = "info";
// * Security, Compression & Parser
app.use(helmet());
app.use(hpp());
app.use(cors());
app.use(compression());
app.use(express.json());
app.use(express.urlencoded({ extended: true }));
// * Http Logger
morgan.token("time", (req) => {
let user = "anonym";
if (req.user) {
if (req.user.name) {
user = req.user.name || "anonym";
}
}
const time = dayjs().format("h:mm:ss A") + " - " + user;
return time;
});
app.use(morgan("morgan: [:time] :method :url - :status"));
// * Paginate
app.use(paginate.middleware(10, 30));
// * Route
app.use(require("./routes"));
// * Custom Error Handler
app.use(errorHandler);
// * Rolling Log
let layoutConfig = {
type: "pattern",
pattern: "%x{id}: [%x{info}] %p %c - %[%m%]",
tokens: {
id: () => {
return Date.now();
},
info: (req) => {
const info = dayjs().format("D/M/YYYY h:mm:ss A");
return info;
},
},
};
log4js.configure({
appenders: {
app: {
type: "dateFile",
filename: "./logs/app.log",
numBackups: 3,
layout: layoutConfig,
maxLogSize: 7000000, // byte == 3mb
},
console: {
type: "console",
layout: layoutConfig,
},
},
categories: {
default: { appenders: ["app", "console"], level: "debug" },
},
});
// * Server Listen
app.listen(PORT, (err) => {
if (err) {
log.error(`Error : ${err}`);
process.exit(1);
}
log.info(`Server is Running On Port : ${PORT}`);
});
module.exports = app;