nodejs和nginx可以反向代理解决跨域问题。Localserviceconstexpress=require('express')constapp=express()//如果在最上面,以/开头的都会被拦截app.get('/',(req,res)=>res.send('HelloWorld!'))app.use(express.static('public'));//静态资源app.use('/dist',express.static(path.join(__dirname,'public')));//静态资源//404app.use('/test',function(req,res,next){res.status(404).send("抱歉找不到那个!");});app.use(function(req,res,next){//TODO中间件,每个请求都会经过next();});app.use(function(err,req,res,next){//TODO失败的中间件,请求错误后,会经过console.error(err.stack);res.status(500).send('Somethingbroke!');next();});app.listen(4000,()=>console.log('Exampleapplisteningonport4000!'))与请求一起使用以代理来自其他服务器的请求constrequest=require('request');app.use('/base/',function(req,res){leturl='http://localhost:3000/base'+req.url;req.pipe(request(url)).pipe(res);});使用http-proxy-middlewareconsthttp_proxy=require('http-proxy-middleware');constproxy={'/tarsier-dcv/':{target:'http://192.168.1.190:1661'},'/base/':{target:'http://localhost:8088',pathRewrite:{'^/base':'/debug/base'}}};for(letkeyinproxy){app.use(key,http_proxy(proxy[key]));}监听本地文件变化使用nodemon插件--watchtest监听test文件夹下的所有文件根目录,如果有变化,会重启服务。“脚本”:{“服务器”:“nodemon--watchbuild--watchtestsrc/server.js”}
