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