-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
58 lines (49 loc) · 1.78 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
// require modules, create an express app
const path = require('path');
const express = require('express');
const app = express();
const PORT = process.env.PORT || 3000;
// import express routers
const indexRouter = require('./routes/index.js');
const projectsRouter = require('./routes/projects.js');
// import json data
app.locals = require('./importData.js');
// my own custom console and error logging module
const message = require('./routes/message.js');
// setting html rendering engine to pug
app.set('view engine', 'pug');
// telling the express app to use specific internal folder paths
app.use('/static', express.static(path.join('./', 'public')));
app.use('/img', express.static(path.join('./','img')));
app.use('/projects/static/', express.static(path.join('./', 'public')));
// telling the express app which router to use for valid paths
app.use(indexRouter);
app.use(projectsRouter);
app.use('/', indexRouter);
app.use('/about', indexRouter);
app.use('/project', projectsRouter);
app.use('/projects', projectsRouter);
app.use('/project/:id', projectsRouter);
app.use('/projects/:id', projectsRouter);
// telling express what to use for any paths that are not matched above
app.use((req, res, next) => {
const err = new Error();
err.status = 404;
err.message = `It seems we can't find that page or path`;
next(err);
});
// an error handler
app.use((err, req, res, next) => {
const routeErr = err;
if (err.status == 404 ){
message.logError(message.status.routeStatus, err.status, err.message, err.stack);
res.status(err.status);
res.locals = app.locals;
res.locals.routeErr = routeErr;
res.render('error', routeErr);
err.status = 0;
}
});
app.listen(PORT, () => {
message.log( `${message.status.running}${PORT}` );
});