-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathapp.js
58 lines (48 loc) · 1.16 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
const express = require('express');
const handlebars = require('express-handlebars');
const fs = require('fs')
const prompts = require('./prompts.js');
const app = express();
const port = process.env.PORT || 5500;
app.set('view engine', 'hbs');
app.engine(
'hbs',
handlebars({
layoutsDir: __dirname + '/views/layouts',
extname: 'hbs',
defaultLayout: 'index',
partialsDir: __dirname + '/views/partials/',
}),
);
app.use(express.static('dist'));
app.use(express.static('public'));
const piecesWithFilenames = {};
prompts.forEach((p, i) => {
const key = `day${i+1}`;
piecesWithFilenames[key] = {
name: p,
file: key
};
});
app.get('/:route', (req, res) => {
const piece = piecesWithFilenames[req.params.route] ;
if (piece) {
res.render('piece', {
name: piece.name,
scriptName: `${piece.file}.js`
});
} else {
res.status(404).send('Not found');
}
});
app.get('/', (_req, res) => {
res.render('index', {
pieces: Object.values(piecesWithFilenames)
});
});
app.get('*', (req, res) => {
res.status(404).send('Not found');
});
app.listen(port, () => {
console.log(`Server running on port ${port}`);
});