后端跨域之二:http-proxy-middleware
自己搞一个反向代理服务器 中间键
const proxy = require(‘http-proxy-middleware’)
let apiProxy = proxy('api',{
target: 'http://www.example.org',
changeOrigin: true
})
在这里插入代码片/api : 暗号
target: 配置
const http = require('http')
const proxy = require('http-proxy-middleware')
http.createServer((req,res)=>{
let url = req.url
res.writeHead(200,{
'Access-Control-Allow-Origin': '*'
})
if(/^\/api/.test(url)){
let apiProxy = proxy('api',{
target: 'https://m.lagou.com',
changeOrigin: true,
pathRewrite: {
'^/api': ''
}
})
apiProxy(req,res)
}else{
res.end('ok')
}
}).listen(8080)``