-
Notifications
You must be signed in to change notification settings - Fork 1
/
app.js
executable file
·70 lines (59 loc) · 1.72 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
69
70
const path = require("path"); // for __dirname
const express = require("express");
const logger = require("morgan");
const favicon = require("serve-favicon");
const bodyParser = require("body-parser");
const createError = require("http-errors");
const data = require("./data.json");
// Instantiate the app instance
const app = express();
// Set view engine
app.set("view engine", "pug");
// Set view directory
app.set("views", path.join(__dirname, "views"));
// Log requests
app.use(logger("dev"));
// Serve favicon
app.use(favicon(path.join(__dirname, "public", "favicon.ico")));
// Parse json
app.use(bodyParser.json());
// Parse form data
app.use(bodyParser.urlencoded({ extended: false }));
// Set public directory
app.use("/static", express.static(path.join(__dirname, "public")));
app.use(function (err, req, res, next) {
console.error(err.stack);
res.status(500).send('Something broke!');
})
/*
ROUTES
*/
// Render a home page
app.get("/", function(req, res) {
res.render("index", { data });
});
// Render an about page
app.get("/about", function(req, res) {
res.render("about");
});
// Render project pages
app.get("/projects/:id", function(req, res) {
const project = data.projects.filter( proj => req.params.id === proj.id )[0];
res.render("project", { project });
});
// Catch 404s and forward to error handler from http-errors
app.use(function(req, res, next) {
next(createError(404));
});
// error handler
app.use(function(err, req, res, next) {
// Set message to err.message
res.locals.message = err.message;
// Set status to err.status
res.locals.status = err.status || 500
// Set the response status to err status or 500
res.status(err.status || 500);
// Render the error page
res.render("error");
});
module.exports = app;