-
Notifications
You must be signed in to change notification settings - Fork 3
/
app.js
54 lines (46 loc) · 1.37 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
import 'dotenv/config';
import express from 'express';
import mustacheExpress from 'mustache-express';
import session from 'express-session';
import MongoStore from 'connect-mongo';
import cookieParser from 'cookie-parser';
import compression from 'compression';
import AppRouter from './routes/App.js';
import UserRouter from './routes/User.js';
import injectUser from './app/middlewares/injectUser.js';
const PORT = process.env.PORT || 3000;
const app = express();
app.engine('mustache', mustacheExpress('views/partials/', '.mustache'));
app.set('view engine', 'mustache');
app.set('views', 'views');
// noinspection JSCheckFunctionSignatures
app.use(compression());
app.use(express.static('public'));
app.use(express.urlencoded({ extended: true }));
app.use(express.json());
app.use(cookieParser());
const oneDay = 1000 * 60 * 60 * 24;
app.use(
session({
secret: process.env.SESSION_SECRET,
store: MongoStore.create({
mongoUrl: process.env.MONGODB_URL,
}),
saveUninitialized: true,
cookie: { maxAge: oneDay },
resave: false,
})
);
app.use(injectUser);
app.use('/', AppRouter);
app.use('/user', UserRouter);
app.get('*', (req, res) => {
res.status(404);
res.render('404', {
alert: {
title: '404 not found',
body: 'This page has not been found. Sorry!',
},
});
});
app.listen(PORT, () => console.log(`Server started on port: ${PORT}`));