Using the node set up a proxy server to solve cross-domain problems during the first back-end docking

One problem encountered recently in development: I do on my machine the page, when I use ajax request access to another back-end server machine, due to the question of cross-domain data can not be docked to debug, and later use node set up a proxy server to solve this problem, specifically record it.

First statement: This only applies to the use of front and back end interface for debugging purposes, for other cases may correspond Baidu own solutions, back-end solutions to personal recommendation, you only need to modify the response header to a variety of front-end treatment there are more cumbersome and limitations.

Code on it:

// 首先你的安装对应的依赖包 express以及request包

const express=require('express');
const request=require('request');

const app=express();
app.use(express.static('静态页面路径')); // 放置你的静态页面项目

app.post('status',function(req,res){
    var url="后端IP:端口/status";
    req.pipe(request(url)).pipe(res);
}) 

// 对于不同的请求需要重复上述代码


app.listen(3000,()=>console.log('start server in port 3000'))

At this point you visit locahost: 3000 / status, the agency will be to the rear end of the interface status, then there would not be a cross-domain problems

Code is a code down, there is the wrong place, please correct me.

over

Published 47 original articles · won praise 38 · views 60000 +

Guess you like

Origin blog.csdn.net/qq8241994/article/details/90081629