-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
36 lines (27 loc) · 920 Bytes
/
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
const express = require('express');
const app = express();
const bodyParser = require('body-parser');
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));
const port = process.env.PORT || 8080;
const token = process.env.TOKEN || 'DEMO_KEY';
app.use(express.static(__dirname));
app.get('/', (req, res) => {
res.sendFile('index.html');
});
// If HTTP request is made to '/token' route without custom header name
// Redirect to homepage. This prevents users from directly visiting the route
app.use('/token', (req, res, next) => {
if (!req.headers['x-custom-header-name']) {
res.redirect('/');
}
next();
});
app.get('/token', (req, res) => {
res.json({'token': token});
});
// If user tries to visit non-existent routes, redirect to hompage.
app.use((req, res, next) => {
res.status(404).redirect('/');
});
app.listen(port, () => console.log(`App is running on port: ${port}`));