-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
executable file
·78 lines (63 loc) · 1.83 KB
/
main.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
69
70
71
72
73
74
75
76
77
78
//loads in all the required packages (install them with the command "npm install")
const express = require("express");
const path = require("path");
const nunjucks = require("nunjucks");
const scratch = require("./scratch");
const store = require("./store");
const news = require("./news")
const secrets = require("./secrets1")
//load and initate things
const app = express();
const menuItems = [
{ title: "Home", path: "/" },
{ title: "News", path: news.basePath},
{ title: "HTML", path: "/html/" },
{ title: "Scratch", path: scratch.basePath }
];
//initate templating engine
nunjucks
.configure(path.join("data", "html"), {
autoescape: true,
express: app
})
.addGlobal("menuItems", menuItems);
//allows client to read files in the "data" folder
app.use(express.static(path.join(__dirname, "data")));
//sets the value of req.path to the requested page
app.use((req, res, next) => {
res.locals.activePath = req.path;
next();
});
//sends homepage
app.get("/", (req, res) => {
res.render("home.html");
});
//sends a selectable list of the ninja created pages
app.get("/html", (req, res) => {
store.users.find({}, (err, docs) => {
res.render("list.html", {
items: docs,
title: "Webpages",
shortlink: "/ninjas/"
});
});
});
//sends general information about creators and source code
app.get("/info", (req, res) => {
res.render("info.html");
});
app.use(secrets.basePath, secrets.routes);
app.use(scratch.basePath, scratch.routes);
app.use(news.basePath, news.routes);
app.use(store.basePath, store.routes);
//if the requested page is not found send them the error page
app.get("*", (req, res) => {
store.dogs.find({}, (err,docs) => {
res.render("error.html", {links: docs});
})
});
//startup the server
const server = app.listen(process.env.PORT || 8080, () => {
console.log("Running");
console.log(server.address());
});