-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
46 lines (36 loc) · 1.17 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
var express = require('express');
var path = require('path');
var cookieParser = require('cookie-parser');
var logger = require('morgan');
var app = express();
app.use(logger('combined'));
app.use(express.json());
app.use(express.urlencoded({ extended: false }));
app.use(cookieParser());
app.use(express.static(path.join(__dirname, 'public')));
const ALLOWED_BASES = [
'https://www.phizone.cn',
'https://www.phi.zone',
'https://insider.phizone.cn',
];
app.use((req, res, next) => {
let url = new URL(req.protocol + '://' + req.get('host') + req.originalUrl);
let target = url.searchParams.get('uri');
if (target && ALLOWED_BASES.some((e) => target.startsWith(e))) {
url.searchParams.delete('uri');
let targetUrl = new URL(target);
for (let [key, value] of url.searchParams.entries()) {
targetUrl.searchParams.append(key, value);
}
return res.redirect(targetUrl.href);
} else {
return res.redirect(ALLOWED_BASES[0]);
}
});
app.use((err, req, res, next) => {
res.locals.message = err.message;
res.locals.error = req.app.get('env') === 'development' ? err : {};
res.status(err.status || 500);
res.render('error');
});
module.exports = app;