-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
55 lines (42 loc) · 1.14 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
const express = require('express');
const app = express();
require('./controllers/api')(app);
app.use(function(req, res, next) {
res.header('Access-Control-Allow-Origin', '*');
res.header(
'Access-Control-Allow-Headers',
'Origin, X-Requested-With, Content-Type, Accept'
);
next();
});
app.use('/', express.static(__dirname + '/ui'));
app.use('*', (req, res, next) => {
let ref = req.get('referer');
if (ref) {
let path = ref
.replace(req.get('host'), '')
.replace(new RegExp('http://|https://'), '');
let redirectPath = path.substring(0, path.length - 1) + req.originalUrl;
if (!req.originalUrl.includes(path)) res.redirect(redirectPath);
}
next();
});
app.use(express.static(__dirname + '/public'));
app.use((req, res, next) => {
res.status(404);
// respond with html page
if (req.accepts('html')) {
res.redirect('/');
return;
}
// respond with json
if (req.accepts('json')) {
res.send({ error: 'Not found' });
return;
}
// default to plain-text. send()
res.type('txt').send('Not found');
});
app.listen(8080, function() {
console.log('server listening on port 8080!');
});