-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
executable file
·74 lines (65 loc) · 1.68 KB
/
app.js
File metadata and controls
executable file
·74 lines (65 loc) · 1.68 KB
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
const express = require("express");
const nconf = require("nconf");
const chalk = require("chalk");
const bodyParser = require("body-parser");
const cors = require("cors");
const morgan = require("morgan");
const app = express();
const { createLogger, format, transports } = require("winston");
// require bodyparser
app.use(bodyParser.json());
app.use(
bodyParser.urlencoded({
extended: true,
})
);
// set cross origin requests
app.use(cors());
// logger
app.use(morgan("tiny"));
const logger = createLogger({
level: "debug",
format: format.simple(),
transports: [new transports.Console()],
});
// load config file
nconf
.argv()
.env()
.file({
file: __dirname + "/config.json",
});
// disable some headers
app.disable("etag");
app.disable("x-powered-by");
app.use(express.static(__dirname + "/static"));
app.use(express.static(__dirname + "/public"));
app.use(express.static(__dirname + "/public/static"));
app.get("/", (req, res) => {
res.sendFile("index.html");
});
app.use(require("./routes/verify"));
app.use("/api", require("./routes"));
//override console log with winston
// console.log = (...args) => {
// logger.info(...args);
// };
// 404
app.use(function (req, res, next) {
res.status(404).render("404");
});
// error handling routes
app.use((err, req, res, next) => {
console.log(err);
res.status(500).json({ error: true, message: "Unexpected error occurred" });
});
// start the app
console.log(`${chalk.green("✓")} Starting the program...`);
app.listen(process.argv[2] || nconf.get("port"), () => {
console.log(
`${chalk.green("✓")} App is running at http://localhost:${nconf.get(
"port"
)}`
);
console.log(" Press CTRL-C to stop\n");
});