-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
53 lines (44 loc) · 1.44 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
const express = require("express");
const path = require("path");
const cookieParser = require("cookie-parser");
const logger = require("morgan");
const helmet = require('helmet');
const cors = require('cors');
const fs = require('fs');
const https = require("https");
const privateKey = fs.readFileSync('./sslcert/cert.key','utf8');
const certificate = fs.readFileSync('./sslcert/cert.pem','utf8');
const credentials = {
key: privateKey,
cert: certificate
};
const indexRouter = require("./routes/stockRouter");
const usersRouter = require("./routes/usersRouter");
const swaggerRouter = require("./routes/swaggerRouter");
const app = express();
app.use(cors());
app.use(helmet());
app.use(logger("common"));
app.use(express.json());
app.use(express.urlencoded({ extended: false }));
app.use(cookieParser());
app.use(express.static(path.join(__dirname, "public")));
// set up routers
app.use("/stocks", indexRouter);
app.use("/user", usersRouter);
app.use("/", swaggerRouter);
// 404 handling
app.use(function (req, res, next) {
res.status(404).json({error: true, message: "route not found"});
});
// set up logging
logger.token("req", (req, res) => JSON.stringify(req.headers));
logger.token("res", (req, res) => {
const headers = {};
res.getHeaderNames().map((h) => (headers[h] = res.getHeader(h)));
return JSON.stringify(headers);
});
// create https server
const server = https.createServer(credentials, app);
server.listen(443);
module.exports = app;