-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
69 lines (54 loc) · 1.5 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
59
60
61
62
63
64
65
66
67
68
69
const express = require('express');
const bodyParser = require('body-parser');
const glob = require('glob');
const session = require('express-session');
const MySQLStore = require('express-mysql-session')(session);
const flash = require('connect-flash');
const compression = require('compression');
const path = require('path');
require('dotenv').config();
const app = express();
const PORT = process.env.PORT || 3000;
app.use(compression());
app.use(express.static(path.resolve('public')));
app.use('/uploads', express.static(path.join(__dirname, 'uploads')));
app.set('view engine', 'ejs');
app.set('views', 'views');
const store = new MySQLStore({
host: process.env.DATABASE_HOST,
port: 3306,
user: process.env.DATABASE_USER,
password: process.env.DATABASE_PASSWORD,
database: process.env.DATABASE_NAME,
});
app.use(
session({
secret: 'my secret',
resave: false,
saveUninitialized: false,
store,
})
);
app.use(flash());
app.use(bodyParser.urlencoded({ extended: false }));
app.use((req, res, next) => {
res.locals.user = req.session.user;
next();
});
glob.sync('./routes/*.js').forEach((file) => {
app.use(require(path.resolve(file)));
});
// 404 Catch all
app.use((req, res, next) => {
res.status(404).render('404');
});
// 500 Catch All
app.use((error, req, res, next) => {
console.log(error);
res.status(500).render('500');
});
app.listen(PORT, () =>
console.log(
`🚀 Server started in ${process.env.NODE_ENV} mode at http://${process.env.HOST}:${PORT}`
)
);