1. 程式人生 > >Nodejs http-proxy代理實戰應用

Nodejs http-proxy代理實戰應用

port proxy delete eat tar rip you app this

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);});

Nodejs http-proxy代理實戰應用