nodejs 配置代理服务器

const express = require('express');
const proxy = require('http-proxy-middleware');
const cors = require('cors');
var app = express();


app.use(cors({
    credentials: true, 
    optionsSuccessStatus: 200,    
    origin: 'http://localhost:8080' // 设置为项目所在服务器目录(允许跨域请求的网址)
}));



app.use('/api', proxy({
    target:'http://192.168.31.65:8000/', // 服务器api地址目录
    changeOrigin: true,
    pathRewrite:{
        "^/api":""
    }
}));

app.listen(3000,'192.168.31.65', function(){ // 代理接口
    console.log('代理接口启动成功');
})

// 例如 api接口为 http://192.168.31.65:8000/api/getid/a
// 实际应该请求的地址为 http://192.168.31.65:3000/api/getid/a

猜你喜欢

转载自www.cnblogs.com/litings/p/10575814.html