码迷,mamicode.com
首页 > Web开发 > 详细

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

时间:2016-06-27 15:13:02      阅读:175      评论: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
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!