-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathproxy.js
34 lines (31 loc) · 939 Bytes
/
proxy.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
const http = require('http')
const httpProxy = require('http-proxy')
const proxy = httpProxy.createProxyServer({})
const server = http.createServer(function (req, res) {
// You can define here your custom logic to handle the request
// and then proxy the request.
const { headers, method, url } = req
console.log('Proxy evoke', headers.host, method, url)
// Proxy to demo.com
if (headers.host.match(/^demo\.com$/i)) {
proxy.web(req, res, { target: 'http://127.0.0.1:3000' }, error => {
console.error(error.message)
res.statusCode = 504
res.end()
})
return
}
// Proxy to api.demo.com
if (headers.host.match(/^api\.demo\.com$/i)) {
proxy.web(req, res, { target: 'http://127.0.0.1:4000' }, error => {
console.error(error.message)
res.statusCode = 504
res.end()
})
return
}
res.statusCode = 404
res.end()
})
console.log('Proxy running.')
server.listen(80)