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

 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM