http-proxy-middleware
http-proxy-middleware official document: https://www.npmjs.com/package/http-proxy-middleware
Chinese document: https://www.cnblogs.com/zhaoweikai/p/9969282.html
http-proxy-middleware for forwarding requests to other servers.
eg:
var express = require('express');
var proxy = require('http-proxy-middleware');
Ideas were Out = ();
app.use('/api', proxy({target: 'http://10.119.168.87:4000', changeOrigin: true}));
app.listen(3000);
Note: We use the express port in 3000 started a small server, use of app.use('/api', proxy({target: 'http://10.119.168.87:4000/', changeOrigin: true}))
these words, to make the hair / api request to forward port 3000 to port 4000. I.e., the request http://localhost:3000/api
corresponds to a request http://10.119.168.87:4000 /api
.
Middleware:
app the mounted "middleware" and "routing" in app._router.stack this array variable inside each "middleware," or "route" is an object, they exist app in the form of array elements. _router inside.
"Middleware", or "routing" of the object structure
// This is a express.static (); middleware app._router.stack data structure stored inside // pathToRegexp = the require ( "path-to-regexp"); { handle: [Function: serveStatic] // intermediate member callback function, such as: app.use (express.static ( "/ public ")), then the handle where express.static = ( "/ public") name: 'serveStatic' , // function name middleware, or routing function name, assuming middleware fn, then = fn.name name params: undefined, path: undefined, Keys: [], regexp: ? / ^ \ / Theme \ / (? = \ / | $) / i , // path matching, such as: app.use ( "/ static", express.static ( "/ public")); where a = pathToRegexp regexp ( "/ static"); route: undefined }
1. The destruction of middleware, the middleware to find the corresponding objects, remove elements, 2. replace middleware, the middleware to find the corresponding object, overwriting handle, regexp attribute 3. dynamically add, the old way, using app.use ()
Project:
Acting out all the json data: json format
{ name:'itc', ip:'10.33.69.76', port:'8080' }
For example, access to local address: http: //127.0.0.1/itc will delegate to the http://10.33.69.76:8080/itc
Because helpful to CRUD functionality, so the dynamic modification json data, to be re-binding agent
// proxyClear middleware empty, added to prevent duplication function proxyClear () { the let Stack = app._router.stack; for (I = the let-stack.lenght. 1; I> = 0; i-- ) { the let Item = Stack [ I]; IF (item.name, for === "middleware") { // middleware added to the name "middleware" stack.splice (I,. 1 ) } } }
Add Agent
// proxyTable data agent function proxyTable (Data) { // middleware before emptying proxyClear (); for (index in Data) { the let Item = Data [index]; the let name = '/' + item.name, for; the let IP = 'HTTP: //'+item.ip+': '+ item.port; app.use (name, Proxy ({ target: IP, changeOrigin: to true })) } }
app.use ( '/ api', proxy (options)), corresponding to the local server listens to client requests '/ api' interface, the execution is returned to the upper side middleware middleware functions, it can be seen from the top, this function will forward the request to the proxy server.
Last unsuccessful proxy: Proxy Address Access 404
The reason: because the functions are more involved, so a separate written document, there are using the app alone in this sub-file, the last out of the app export and import in the coming index.js file and sub-files app.use .
Change: Adding intermediate listening port, listening subfile in the subfile, a port defined in the subfile defined by the intermediate sub-file (3000) to access, in the interface definition of the subfile 'get' through the parent file listening port (8080) to visit.
index.js
var express = require("express"); var app = express(); var example = require('../router/example');//子文件 app.use('/example',example)
app.listen(8080)
example.js
var express = require("express"); var app = express();
// proxyClear middleware empty, added to prevent duplication function proxyClear () {the let Stack = app._router.stack; for (I = the let-stack.lenght. 1; I> = 0; i-- ) {the let Item = Stack [ I]; IF (item.name, for === "middleware") { // middleware added to the name "middleware" stack.splice (I,. 1 )}}}
// proxyTable data agent function proxyTable (Data) { // middleware before emptying proxyClear (); for (index in Data) { the let Item = Data [index]; the let name = '/' + item.name, for; the let IP = 'HTTP: //'+item.ip+': '+ item.port; app.use (name, Proxy ({ target: IP, changeOrigin: to true })) } }
app.get (' / GET ', function (REQ, RES) {
proxyTable (data);
// set http status response, and sends the browser data
res.status (200 is) .send (data);
})
// subfolders app needs to have a port used to access the middleware
app.listen (3000)
//这是一个express.static();中间件在app._router.stack里面存储的数据结构
//pathToRegexp=require("path-to-regexp");
{
handle: [Function: serveStatic]
//中间件回调函数,比如: app.use(express.static("/public")),那么这里的handle=express.static("/public")
name: 'serveStatic',
//中间件的函数名,或者路由的函数名,假设中间件为 fn,那么name=fn.name
params: undefined,
path: undefined,
keys: [],
regexp: /^\/theme\/?(?=\/|$)/i,
//匹配的路径,比如: app.use("/static",express.static("/public"));这里的regexp=pathToRegexp("/static");
route: undefined
}
Know the data storage structure of middleware, we can easily destroy the middle or replace middleware, or realize his app.unuse () function of the
Specific operations
1. 销毁中间件,找到对应的中间件对象,删除元素,
2. 替换中间件,找到对应的中间件对象,重写handle,regexp属性
3. 动态添加,老办法,使用app.use()
//这是一个express.static();中间件在app._router.stack里面存储的数据结构
//pathToRegexp=require("path-to-regexp");
{
handle: [Function: serveStatic]
//中间件回调函数,比如: app.use(express.static("/public")),那么这里的handle=express.static("/public")
name: 'serveStatic',
//中间件的函数名,或者路由的函数名,假设中间件为 fn,那么name=fn.name
params: undefined,
path: undefined,
keys: [],
regexp: /^\/theme\/?(?=\/|$)/i,
//匹配的路径,比如: app.use("/static",express.static("/public"));这里的regexp=pathToRegexp("/static");
route: undefined
}
Know the data storage structure of middleware, we can easily destroy the middle or replace middleware, or realize his app.unuse () function of the
Specific operations
1. 销毁中间件,找到对应的中间件对象,删除元素,
2. 替换中间件,找到对应的中间件对象,重写handle,regexp属性
3. 动态添加,老办法,使用app.use()