首先須要 npm install express 和 npm install requestexpress
代碼以下:npm
const express = require('express'); const path = require('path'); const app = express(); const request = require('request'); let serverUrl = 'http://192.9.104.116:8080';//代理server地址 // 配置靜態文件服務中間件 app.use(express.static(path.join(__dirname, 'src'))); app.use('/', function (req, res) { //跨域處理 res.setHeader('Access-Control-Allow-Origin', '*'); //代理接口地址 let url = serverUrl + req.url; //用request轉發代理 req.pipe(request(url)).pipe(res); }); app.listen(3000, () => { console.log(`App listening at port 3000`) })
但願對你們有必定幫助…………跨域