首页 > Web开发 > 详细

nodejs 配置代理服务器

时间:2019-03-22 01:06:14      阅读:296      评论:0      收藏:0      [点我收藏+]
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

 

nodejs 配置代理服务器

原文:https://www.cnblogs.com/litings/p/10575814.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!