This repository has been archived by the owner on May 31, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathserver.js
110 lines (94 loc) · 2.33 KB
/
server.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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
/**
* Import base packages
*/
const express = require('express');
const app = express();
const bodyParser = require('body-parser');
const browsersupport = require('express-browsersupport');
/**
* Import own packages
*/
const config = require('./config/config');
const webRouter = require('./routers/Web');
const apiRouter = require('./routers/Api');
const indexController = require('./controllers/Web/IndexController');
const tradfri = require('./helpers/modules/Tradfri');
/**
* Init the Tradfri module
*/
tradfri.init();
/**
* Set template engine
*/
app.set('view engine', 'ejs');
app.set('views', `${__dirname}/views`);
/**
* Serve static public dir
*/
app.use(express.static(`${__dirname}/../public`));
/**
* Configure app to use bodyParser()
*/
app.use(bodyParser.urlencoded({extended: true}));
app.use(bodyParser.json());
/**
* Configure app to use Browser Support
*/
app.use(browsersupport({
redirectUrl: "/oldbrowser",
supportedBrowsers: config.application.supportedBrowsers
}));
/**
* Configure routers
*/
app.use('/', webRouter.router);
app.use('/api', apiRouter.router);
/**
* Render sitemap.xml and robots.txt
*/
app.get('/sitemap.xml', (req, res) => {
indexController.siteMapAction(req, res, webRouter.routes);
});
app.get('/robots.txt', (req, res) => {
indexController.robotsAction(req, res);
});
/**
* Render old browser page
*/
app.get('/oldbrowser', (req, res) => {
indexController.oldBrowserAction(req, res);
});
/**
* Setup default 404 message
*/
app.use((req, res) => {
res.status(404);
// respond with json
if (req.originalUrl.split('/')[1] === 'api') {
/**
* API 404 not found
*/
res.send({error: 'This API route is not implemented yet'});
return;
}
indexController.notFoundAction(req, res);
});
/**
* Disable powered by header for security reasons
*/
app.disable('x-powered-by');
/**
* Start listening on port
*/
const server = app.listen(config.application.port, config.application.bind, () => {
console.log(`[NODE] App is running on: ${config.application.bind}:${config.application.port}`);
});
/**
* Handle nodemon shutdown
*/
process.once('SIGUSR2', () => {
server.close(() => {
console.log(`[NODE] Express exited! Port ${config.application.port} is now free!`);
process.kill(process.pid, 'SIGUSR2');
});
});