Node http-proxy and express

怎甘沉沦 提交于 2019-11-28 05:02:19
Chandler

Using http-proxy 1.0 with 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' });
});

A very straightforward solution which works seamlessly, and with cookies/authentication as well, using 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;
    }
});

And then simply:

app.use("/blog/*", blogProxy);

I know I'm late to join this party, but I hope this helps someone.

I got this working.

  • Install Ghost and make sure it's working property (default port is 2368)
  • Create your node web app using express (listen on port 80) - nothing special here
  • Install node-http-proxy npm install http-proxy in your web app
  • Create wildcard route for /blog* that proxies requests to Ghost service

    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  
      });
    });
    
  • Update the Ghost config to use a sub directory (only supported in 0.4.0+)

    config = {
      // ### Development **(default)**
      development: {
      // The url to use when providing links to the site, E.g. in RSS and email.
      url: 'http://127.0.0.1/blog',
    ...
    
  • You should now be able to hit http://yoursite.com/blog and all routes work.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!