首页 > Web开发 > 详细

使用http-proxy替代nginx实现反向代理

时间:2016-06-27 15:11:10      阅读:265      评论:0      收藏:0      [点我收藏+]
var proxy = require(‘http-proxy‘).createProxyServer({});
proxy.on(function(err, req, res) {
    res.writeHead(500, {
        ‘Content-Type‘: ‘text/plain‘
    });
});
var server = require(‘http‘).createServer(function(req, res) {
    var host = req.headers.host;
    switch (host) {
    case ‘demo.maomao.gift‘:
        proxy.web(req, res, {
            target: ‘http://localhost:3000‘
        });
        break;
    case ‘famanoder.com‘:
        proxy.web(req, res, {
            target: ‘http://localhost:4030‘
        });
        break;
    default:
        res.writeHead(200, {
            ‘Content-Type‘: ‘text/plain‘
        });
        res.end(‘Welcome to my server!‘);
    }
});
console.log("listening on port 80") server.listen(80);

  

使用http-proxy替代nginx实现反向代理

原文:http://www.cnblogs.com/delicious28/p/5619941.html

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