节点http代理和expression
我正在尝试做这样的事情:
// 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')); });
基本上,我想仍然使用快递…但是,当人们去http://localhost/blog
拿到博客,但仍然通过port 8080
(这将最终是端口80)
所以我把它切换到这个,它运作得更好。 问题是,快递接pipe路由(从我可以告诉)
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')); });
使用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' }); });
一个非常简单的解决scheme,可以无缝地运行,并且使用express-http-proxy
也可以使用cookies /
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);
我知道我迟到了参加这个派对,但我希望这可以帮助到一个人。
我得到了这个工作。
- 安装Ghost ,并确保它的工作属性(默认端口是2368)
- 用express来创build你的节点web应用(在80端口上监听) – 这里没什么特别的
- 在你的web应用程序中安装node-http-proxy
npm install http-proxy
-
为代理请求到Ghost服务的/ blog *创build通配符路由
var httpProxy = require('http-proxy'); var proxy = new httpProxy.RoutingProxy(); app.get('/blog*', function (req, res, next) { proxy.proxyRequest(req, res ,{ host: 'moserlap.splitvr.com', port: 2368 }); });
-
更新Ghostconfiguration以使用子目录(仅支持0.4.0以上)
config = { // ### Development **(default)** development: { // The url to use when providing links to the site, Eg in RSS and email. url: 'http://127.0.0.1/blog', ...