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