• Nodejs http-proxy代理实战应用


    var https = require('https');
    var express = require('express');
    var app = express()
    var http = require('http'),
        httpProxy = require('http-proxy');
    var proxy = httpProxy.createProxyServer({ changeOrigin: true});  
    
    app.all('/apis/*', function (req, res, next) {
        var target = req.originalUrl.replace("/apis/", "");
        // Change this API url to suit your project
        var url = `/${target}`;
        req.url = url;
        console.log(url);
        delete req.headers.host;
        proxy.web(req, res, { target: 'http://localhost', changeOrigin: true });  
    })
    
    
    var server = app.listen(8080, function () {
        var host = server.address().address;
        var port = server.address().port;
        console.log('Example app listening at http://%s:%s', host, port);
    });
    

      changeOrigin: true   和  req.url = '处理后的url'

    var https = require('https');var express = require('express');var app = express()var http = require('http'),    httpProxy = require('http-proxy');var proxy = httpProxy.createProxyServer({ changeOrigin: true});  
    app.all('/apis/*', function (req, res, next) {    var target = req.originalUrl.replace("/apis/", "");    // Change this API url to suit your project    var url = `/${target}`;    req.url = url;    console.log(url);    delete req.headers.host;    proxy.web(req, res, { target: 'http://localhost', changeOrigin: true });  })

    var server = app.listen(8080, function () {    var host = server.address().address;    var port = server.address().port;    console.log('Example app listening at http://%s:%s', host, port);});

  • 相关阅读:
    使用vim + cscope/ctags
    python类库32[序列化和反序列化之pickle]
    Perforce2012新特征=20个用户免费+云
    Linux进程的uid和euid
    perl安装模块到自己的home ( install perl module without root)
    Python分布式+云计算
    Linux命令xargs+cut
    python实例32[简单的HttpServer]
    Python转载[编码规范]
    Linux命令lsof
  • 原文地址:https://www.cnblogs.com/guxuelong/p/9009703.html
Copyright © 2020-2023  润新知