-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
54 lines (47 loc) · 1.33 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
const express = require('express');
const app = express();
app.set('view engine', 'pug');
app.use('/static', express.static('public'));
const data = require('./data.json');
app.get('/', (req, res) => {
res.locals = data.projects;
const projects = data.projects;
res.render('index', {projects: projects});
});
app.get('/about', (req, res) => {
res.render('about');
});
app.get(`/projects/:id`, (req, res, next) => {
const id = req.params.id;
const project = data.projects[id];
if (!project) {
const err = new Error;
err.status = 404;
err.message = `Project ${id} does not exist.`;
next(err);
}
res.render('project', project);
});
// catch 404 and forward to error handler
app.use((req, res, next) => {
const err = new Error;
err.status = 404;
err.message = 'Page Not Found';
next(err);
});
// error handler
app.use((err, req, res, next) => {
if (err) {
if (err.status == 404) {
res.status(404);
} else {
err.message = err.message || 'Something went wrong.';
err.status = err.status || 500;
}
console.log(`Global error handler called. Status: ${err.status}. Message: ${err.message}.`);
res.render('error', {err});
}
});
app.listen(3000, () => {
console.log('App is running on 3000!');
});