nodejs+express realizes front-end cross-domain problem based on http-proxy-middleware

The first step: cmd window into the nodejs project directory:

Execute the following command:

$ npm install --save-dev http-proxy-middleware

Let post in server.js file

Write as follows:

const express = require('express');
const app = express();
/******************************** Start reverse proxy **************** ********************/
var proxy = require('http-proxy-middleware');//Introduce the reverse proxy module of nodejs
// proxy middleware options
var options = {
    target: 'http://127.0.0.1:9090', // target host
    changeOrigin: true,               // needed for virtual hosted sites
};
// create the proxy (without context)
var exampleProxy = proxy('/api-sso/*',options);
app.use(exampleProxy);
/******************************** Start reverse proxy **************** ********************/

/**************************** Enable service monitoring****************** ************/
app.listen('3000', function() {
    console.log('Starting project mpmdFront, port: 3000');
});

For example: the front-end server address is 127.0.0.1:3000. If you want to directly access the address resource of http://127.0.0.1:9090/api-sso/*, there will be a cross-domain problem.

This configuration var exampleProxy = proxy('/api-sso/*',options); is to access the /api-sso/* access resource proxy to the address server of http://127.0.0.1:9090/api-sso/* go in.

The ajxa call is as follows:

The above proxy configuration will forward the request of http://127.0.0.1:3000/api-sso/* to http://127.0.0.1:9090/api-sso/* server. To achieve a front-end cross-domain solution. The options inside the detailed configuration

Please refer to the official website: https://www.npmjs.com/package/http-proxy-middleware

 

Where options are parameter options:

target: is the target url to be proxied

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325054063&siteId=291194637