我是node-http-proxy
模块的新手。
我的目标
我需要使用模块为多子域提供多重SSL。
例如;
如果用户呼叫process.localhost:1443
,那么我应该将呼叫路由到process.localhost:2443
和
如果用户呼叫api.localhost:1443
,那么我应该将呼叫路由到api.localhost:3443
正在发生的事情
我写了下面的server.js代码。但是,当我尝试拨打process.localhost:1443
时,我收到以下错误;
D:\Work Space\...\http-proxy\node_modules\requires-port\index.js:13
protocol = protocol.split(':')[0];
TypeError: Cannot call method 'split' of undefined
protocol
似乎是undefined
。
function required(port, protocol) {
protocol = protocol.split(':')[0];
我该怎么办?
server.js
var fs = require('fs'),
httpProxy = require('http-proxy'),
express = require('express'),
app = require('./app').service,
api = require('./api').service;
// PROXY
var options = {
changeOrigin: true,
forward: {
'process.localhost': 'process.localhost:2443',
'api.localhost' : 'api.localhost:3443'
}
}
httpProxy.createServer(options).listen(1443, function() {
console.log('Proxy is listening on port 1443')
})
// HTTP
app
.listen(2443, function() {
console.log('PROCESS APP server is listening on port 2443')
})
api
.listen(3443, function() {
console.log('API APP server is listening on port 3443')
})
答案 0 :(得分:0)
我可以通过node-http-proxy
论坛的人来解决这个问题。
var proxyTable = {}
proxyTable['api.localhost:1443'] = 'http://127.0.0.1:3443'
proxyTable['process.localhost:1443'] = 'http://127.0.0.1:2443'
var proxy = httpProxy.createServer({changeOrigin: true})
var http = require('http')
http.createServer(function(req, res) {
var options = {
target: proxyTable[req.headers.host]
}
proxy.web(req, res, options)
}).listen(1443, function() {
console.log('Proxy server is listening on port 1443')
})
app.listen(2443, function() {
console.log('APP server is listening on port 2443')
})
api.listen(3443, function() {
console.log('API server is listening on port 3443')
})