我的代码
// Setup prox to handle blog requests
httpProxy.createServer({
hostnameOnly: true,
router: {
'http://localhost': '8080',
'http://localhost/blog': '2368'
}
}).listen(8000);
以前我用过这个:
http.createServer(app).listen(app.get('port'), function(){
console.log("Express server listening on port " + app.get('port'));
});
基本上,我还是想用express...但是,当人们去http://localhost/blog
,但仍被送达port 8080
(最终将是80端口)
所以我换了这个,效果更好了。问题是express接管了路由(据我所知)。
var options = {
// pathnameOnly: true,
router: {
'localhost': 'localhost:8080',
'localhost/blog': 'localhost:2368'
}
}
// Setup prox to handle blog requests
var proxyServer = httpProxy.createServer(options);
proxyServer.listen(9000);
require('./app/server/router')(app);
http.createServer(app).listen(app.get('port'), function(){
console.log("Express server listening on port " + app.get('port'));
});
使用express-http-proxy
:
var proxy = require('express-http-proxy');
var blogProxy = proxy('localhost/blog:2368', {
forwardPath: function (req, res) {
return require('url').parse(req.url).path;
}
});
然后:
app.use("/blog/*", blogProxy);
使用HTTP-Proxy 1.0和Express:
var httpProxy = require('http-proxy');
var apiProxy = httpProxy.createProxyServer();
app.get("/api/*", function(req, res){
apiProxy.web(req, res, { target: 'http://google.com:80' });
});