-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
71 lines (58 loc) · 1.95 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
require('dotenv').config();
const express = require('express');
const mssql = require('mssql');
const cors = require("cors");
const helmet = require("helmet");
const morgan = require("morgan");
const swaggerUI = require("swagger-ui-express");
const escape = require('escape-html')
const config = require('./src/config');
const specs = require("./src/utils/swagger");
const app = express();
app.use(express.json({ limit: "1mb" }));
app.use(express.urlencoded({ extended: true }));
// CORS
const corsOptions = {
origin: true,
credentials: true,
methods: ["GET", "POST", "DELETE", "PUT", "PATCH"], // For legacy browser support
};
app.use(cors(corsOptions));
// helmet
app.use(helmet());
app.use((req, res, next) => {
res.header("Strict-Transport-Security", "max-age=63072000; includeSubdomains; preload");
res.header("X-XSS-Protection", "1; mode=block");
res.header("Cache-control", "no-store");
res.header("Pragma", "no-cache");
next();
});
/* Web server request logging */
app.use(
morgan(":method :url :status :res[content-length] - :response-time ms"),
);
/* DB Connection. */
app.use(async function (req, res, next) {
await mssql.connect(config.db_connection())
req.sql = mssql;
next();
});
/* OpenAPI Documentation. */
app.use("/api-docs", swaggerUI.serve);
app.get("/api-docs", swaggerUI.setup(specs, { explorer: true }));
/* Routes */
app.use('/todo', require('./src/routes/todo'));
// catch 404 and forward to error handler
app.use(function (req, res) {
const err = new Error('Not Found: '+ escape(req.method) + ":" + escape(req.originalUrl));
res.status(404).send(err.message);
});
app.set('port', process.env.PORT || 3000);
process.on("uncaughtException", (error) => {
console.error("Uncaught exception", error.message);
console.error(error);
});
const server = app.listen(app.get('port'), function() {
console.log('Express server listening on port ' + server.address().port);
});
module.exports = app;