Nodejs http-proxy代理实战应用

Posted 顾学龙

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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);});

以上是关于Nodejs http-proxy代理实战应用的主要内容,如果未能解决你的问题,请参考以下文章

在本地设置 http-proxy 代理 (前后端分离)

使用节点 http-proxy 代理 websocket 连接

javascript 使用节点http-proxy对快递应用程序的/ api进行代理

Node.js - 使用 Express 和 http-proxy 进行反向代理

#yyds干货盘点# 使用 http-proxy 实现 SAP UI5 请求的代理重定向

使用http-proxy替代nginx实现反向代理