使用express来代理服务的方法

nodejs和nginx都可以反向代理,解决跨域问题。
本地服务

const express = require('express')
const app = express()
 
//如果它在最前面,后面的/开头的都会被拦截
app.get('/', (req, res) => res.send('Hello World!'))
 
app.use(express.static('public'));//静态资源
app.use('/dist', express.static(path.join(__dirname, 'public')));//静态资源
 
//404
app.use('/test', function (req, res, next) {
  res.status(404).send("Sorry can't find that!");
});
 
app.use(function (req, res, next) {
  //TODO 中间件,每个请求都会经过
  next();
});
 
app.use(function (err, req, res, next) {
  //TODO 失败中间件,请求错误后都会经过
  console.error(err.stack);
  res.status(500).send('Something broke!');
  next();
});
 
app.listen(4000, () => console.log('Example app listening on port 4000!'))

与request配合使用
这样就将其它服务器的请求代理过来了

const request = require('request');
app.use('/base/', function (req, res) {
  let url = 'http://localhost:3000/base' + req.url;
  req.pipe(request(url)).pipe(res);
});

使用http-proxy-middleware

const http_proxy = require('http-proxy-middleware');
const proxy = {
 '/tarsier-dcv/': {
  target: 'http://192.168.1.190:1661'
 },
 '/base/': {
  target: 'http://localhost:8088',
  pathRewrite: {'^/base': '/debug/base'}
 }
};
 
for (let key in proxy) {
 app.use(key, http_proxy(proxy[key]));
}

监听本地文件变化
使用nodemon插件。
–watch test指监听根目录下test文件夹的所有文件,有变化就会重启服务。

"scripts": {
 "server": "nodemon --watch build --watch test src/server.js"
}

在这里插入图片描述

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值