-
Notifications
You must be signed in to change notification settings - Fork 48
Expand file tree
/
Copy pathapp.js
More file actions
57 lines (48 loc) · 1.33 KB
/
Copy pathapp.js
File metadata and controls
57 lines (48 loc) · 1.33 KB
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
import express from "express";
import expressHandlebars from "express-handlebars";
import mongoose from "mongoose";
import bodyParser from "body-parser";
import config from "./../config.json";
import router from "./router";
import init from "./init";
import handlebars from "./handlebars";
const app = express();
const port = 3000;
handlebars();
// Connect to Database
const databaseUri = config.database.uri;
const databaseOptions = config.database.options;
mongoose.connect(databaseUri, databaseOptions, async(error) => {
if (error) {
console.error(error);
return;
}
console.log("MongoDB connected");
// Creates Default Data
init();
});
// Set Template Engine
app.engine("handlebars", expressHandlebars({
layoutsDir: __dirname + "/../views/layouts/",
partialsDir: __dirname + "/../views"
}));
app.set("view engine", "handlebars");
// Set Middlewares
app.use(bodyParser.json({ limit: "50mb" }));
app.use(bodyParser.urlencoded({ limit: "50mb", extended: true }));
app.use(express.static("public"));
app.use(router);
// Error Handling 404, 500
app.use((req, res, next) => {
console.warn("404 Page Not Found", req.url);
res.sendStatus(404);
return;
});
app.use((error, req, res, next) => {
console.error(error);
res.sendStatus(500);
return;
});
app.listen(port, () => {
console.log("Server is running on port", port);
});