-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
48 lines (42 loc) · 1.16 KB
/
index.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
import express from 'express';
import { create } from 'express-handlebars';
import cookieParser from 'cookie-parser';
import bodyParser from 'body-parser';
import morgan from 'morgan';
import { PORT, DEBUG } from './src/config.js';
import routes from './src/routes.js';
import { loadCookies } from './src/middleware/auth-middleware.js';
const app = express();
const hbs = create({
defaultLayout: 'main',
extname: '.hbs',
partialsDir: ['src/views/partials/', 'src/views/forms/'],
layoutsDir: 'src/layouts/',
helpers: {
// equality helper
eq(a, b) {
return a == b;
},
},
});
// Middleware
app.use(bodyParser.urlencoded({ extended: true }));
app.use(cookieParser());
app.use(morgan('tiny'));
app.locals.isDebug = DEBUG;
app.use(loadCookies);
// Set view engine to Handlebars
app.engine('.hbs', hbs.engine);
app.set('view engine', '.hbs');
app.set('views', './src/views');
app.use(express.static('public'));
// Use router
app.use('/', routes);
// error handler
app.use((err, req, res, next) => {
console.error(err.stack);
res.status(500).send('Something broke!');
});
app.listen(PORT, () => {
console.log(`listening on http://localhost:${PORT}`);
});