-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
148 lines (137 loc) · 3.62 KB
/
index.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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
var httpProxy = require('http-proxy')
var express = require('express')
var _ = require('lodash')
var $url = require('url')
var path = require('path')
var proxy = httpProxy.createProxyServer({
changeOrigin: true
})
proxy.on('error', function(e){
console.error('proxy error', e)
})
module.exports = function n0gx(conf){
var app = express()
var list = []
_.each(conf, function(val, key){
if (_.isArray(val)) {
list.push([key].concat(val))
} else {
_.each(val, function(v, k){
list.push([k].concat(v).concat(key))
})
}
})
_.each(list, function(val){
var key = val[0]
var type = val[1]
var target = val[2]
var hostn = val[3]
var handler = createHandler(type, target)
if (key === '4xx') {
return app.use(function(err, req, res, next){
if (hostn && req.hostname !== hostn) {
return next(err)
}
if (err.status && err.status < 500) {
handler(req, res, next)
} else next(err)
})
}
if (key === '5xx') {
return app.use(function(err, req, res, next){
if (hostn && req.hostname !== hostn) {
return next(err)
}
if (!err.status || err.status >= 500) {
handler(req, res, next)
} else next(err)
})
}
app.use(key, createSlasher(key), function(req, res, next){
if (hostn && req.hostname !== hostn) {
return next()
}
handler(req, res, next)
})
})
return app
}
function createHandler(type, target){
if (type === 'static') {
return express.static(target)
}
if (type === 'proxy') {
return function(req, res){
forwardReq(req)
// Keeping old syntax as n0gx has been targeting to old versions of Node.js
var proxyTarget = target
var targetObj = $url.parse(target)
if (targetObj.pathname !== '/') {
proxyTarget = [targetObj.protocol, '//', targetObj.host].join('')
// Mutating req.url directly here
req.url = path.posix.join(req.url, targetObj.pathname)
}
proxy.web(req, res, { target: proxyTarget })
}
}
if (type === 'redirect') {
return function(req, res){
xRedirect(req, res, target)
}
}
if (type === 'concat') {
return function(req, res){
var pathname = req._parsedUrl.pathname
pathname = pathname.replace(req.baseUrl, '')
pathname = pathname.replace(/^\//, '')
var _target = target
if (_target.slice(-1) !== '/') _target += '/'
_target = $url.resolve(_target, pathname)
xRedirect(req, res, _target)
}
}
if (type === 'sendfile') {
return function(req, res){
res.sendFile(target, { root: process.cwd() })
}
}
if (type === 'status') {
return function(req, res){
res.status(target).end()
}
}
}
function createSlasher(key){
return function(req, res, next){
var pathname = req._parsedUrl.pathname
if (pathname === '/') return next()
if (key === '/') return next()
if (key.slice(-1) === '/') {
if (pathname === key.slice(0, -1)) {
xRedirect(req, res, key)
} else next()
} else {
if (pathname === key + '/') {
xRedirect(req, res, '..' + key)
} else next()
}
}
}
function xRedirect(req, res, target){
var search = req._parsedUrl.search || ''
res.redirect(target + search)
}
function forwardReq(req){
lowerKeys(req.headers)
if (req.headers['x-forwarded-for']) {
req.headers['x-forwarded-for'] += ', ' + req.ip
} else {
req.headers['x-forwarded-for'] = req.ip
}
}
function lowerKeys(obj){
_.each(obj, function(val, key, list){
delete list[key]
list[key.toLowerCase()] = val
})
}