-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
64 lines (57 loc) · 2.09 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
var createError = require("http-errors");
let express = require("express");
let bodyParser = require("body-parser");
let app = express();
var cors = require("cors");
var logger = require("morgan");
var cookieParser = require("cookie-parser");
var { expressjwt: jwt } = require("express-jwt");
const config = require("./config");
let { userRouter, tdaDataRouter, altDataRouter, mlDataRouter, setup, finModRouter } = require("./routes/index");
/* Parsing the body of the request. */
app.use((req, res, next) => {
if (req.originalUrl === '/api/user/checkout') {
next(); // Do nothing with the body because I need it in a raw state.
} else {
bodyParser.json()(req, res, next); // ONLY do express.json() if the received request is NOT a WebHook from Stripe.
}
});
app.use(bodyParser.urlencoded({ extended: false }));
app.use(cors());
app.use(logger("dev"));
app.use(cookieParser());
/* This is a middleware that checks if the request has a valid token. If it does not, it will return a
401 error. */
app.use(
jwt({
secret: config.Secret,
algorithms: ["HS256"],
credentialsRequired: true,
}).unless({
path: ["/api/setup/status", "/api/user/checkout", "/api/user/register", "/api/user/login", "/api/user/account"]
})
);
app.use("/api/user", userRouter);
app.use("/api/altdata", altDataRouter);
app.use("/api/tdadata", tdaDataRouter);
app.use("/api/finmoddata", finModRouter);
app.use("/api/mldata", mlDataRouter);
app.use("/api/setup", setup);
app.use(function (req, res, next) {
next(createError(404));
});
/* This is a middleware that checks if the request has a valid token. If it does not, it will return a
401 error. */
app.use(function (err, req, res, next) {
if (err.name === "UnauthorizedError") {
res.status(401).send({ code: -1, msg: "Not logged in." });
} else {
res.locals.message = err.message;
res.locals.error = req.app.get("env") === "development" ? err : {};
res.status(err.status || 500);
res.render("error");
}
});
app.listen(3000, function () {
console.log("Listening on port 3000 now.");
});