-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
68 lines (60 loc) · 1.81 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
import express from "express";
import connectDB from "./app/db/connect";
import { join } from "path";
import cors from "cors";
const cookieParser = require("cookie-parser");
const fileUpload = require("express-fileupload");
// custom middlewares
import { insertNavbar } from "@middleware/insertNavbar";
// import routes
import userRoutes from "./app/controller/routes/users";
import pageRoutes from "./app/controller/routes/pages";
import buildRoute from "./app/controller/routes/buildRoute";
import fs from "fs";
//const declare
const port = process.env.PORT || 3000;
const app = express();
//Load view enigne
app.set("views", join(__dirname, "./app/views/pages"));
app.set("view engine", "pug");
//middleware
app.options("*", cors());
app.use(express.static(join(__dirname, "./app/views")));
app.use("scripts/", express.static("/node_modules/"));
app.use(express.json()); //Kan se JSON payloads fra front-end
app.use(express.urlencoded({ extended: false })); //Kan se String/text payloads fra front-end
//app.use("/page/*", insertNavbar);
app.use(cookieParser());
app.use(fileUpload());
//routes
app.get("/", (req, res) => {
res.redirect("/page/login");
res.end();
});
app.use("/api/v1/users", userRoutes);
app.use("/page", pageRoutes);
app.use("/build", buildRoute);
//error handler
app.use((err, req, res, next) => {
console.log("Stack: " + err.stack);
console.log("Message: " + err.message);
console.log("Status: " + err.status);
res.status(err.status || 500);
res.render("error", {
errorCode: err.status,
errorMessage: err.message,
});
});
//server setup
const start = () => {
try {
connectDB();
app.listen(
port,
console.log(`server listening at: http://localhost:${port}`)
);
} catch (error) {
console.log(error.message);
}
};
start();