-
Notifications
You must be signed in to change notification settings - Fork 8
/
setRoutes.js
56 lines (43 loc) · 1.57 KB
/
setRoutes.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
//additional
const express = require("express");
const path = require("path");
const createError = require("http-errors");
const utils = require("./utils/Utils");
const { setCorsHeaders } = utils;
const apiRouter = require("./routes/api");
//const chatRouter = require("./routes/chat");
exports.setRoutes = (app, peerServer) => {
app.use("/peerjsServer", peerServer);
app.use("/api", apiRouter);
//app.use("/chat", chatRouter);
if (process.env.NODE_ENV === "production") {
// Serve any static files
app.use(express.static(path.join(__dirname, "client/build")));
// Handle React routing, return all requests to React app
app.get("*", function (req, res) {
res.sendFile(path.join(__dirname, "client/build", "index.html"));
});
}
// catch 404 and forward to error handler
app.use(function (err, req, res, next) {
next(createError(404, err.toString()));
});
const dbControllerError = require("./controllers/DBcontrollerError");
// error handler
app.use(function (err, req, res, next) {
// set locals, only providing error in development
dbControllerError.insertErrorToDb(err, req).then();
res.locals.message = err.message;
res.locals.error = req.app.get("env") === "development" ? err : {};
// render the error page
res.status(err.status || 500);
if (req.xhr) {
res.send({ error: "Something failed!" + err.toString() });
} else {
//res.render('error',{message:err.toString(),error:err});
setCorsHeaders(req, res);
res.json("Description: " + err.toString());
}
});
return app;
};