-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
66 lines (51 loc) · 1.63 KB
/
main.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
const express = require('express');
const path = require('path');
const http = require('http');
const mongoose = require('mongoose');
const { connectDB } = require('./src/models');
const controllers = require('./src/controllers');
const { forceSsl } = require('./src/middleware');
const env = process.env.NODE_ENV || 'development';
const protocol = process.env.PROTOCOL || 'http';
const port = process.env.PORT || 3000;
const host = process.env.HOST || '0.0.0.0';
const isProduction = env === 'production';
const app = express();
app.set('view engine', 'pug');
app.use(express.json({ limit: '50mb' }));
app.use(
express.urlencoded({
limit: '50mb',
extended: true,
})
);
if (isProduction) {
app.use(forceSsl);
}
app.use(
express.static(path.join(__dirname, 'web', 'public'), { redirect: false })
);
app.get('/sitemap.xml', controllers.sitemap);
app.get('/', controllers.index);
// app.get('/browse', controllers.browse);
app.get('/:slug', controllers.view);
app.get('/preview/:slug', controllers.preview);
app.post('/api/saves', controllers.save);
const server = new http.Server(app);
/* eslint-disable no-console */
server.listen(port, host, async () => {
await connectDB();
console.log(`Server running on ${protocol}://${host}:${port}`);
process.on('SIGTERM', () => {
console.log('SIGTERM signal received.');
console.log('Closing http server...');
server.close(() => {
console.log('Http server closed.');
console.log('Closing MongoDB connection...');
mongoose.connection.close(false, () => {
console.log('MongoDB connection closed.');
process.exit(0);
});
});
});
});