使用 http-proxy 的子域不显示节点 运行 应用程序

subdomains using http-proxy does not display node running app

我正在关注 http://blog.nodejitsu.com/http-proxy-intro/ 编写我的代理服务器以将子域指向不同端口上的节点应用程序 运行ning

var http = require('http'),  
httpProxy = require('http-proxy');

//
// Just set up your options...
//
var options = {  
  hostnameOnly: true,
  router: {
    'localhost': '127.0.0.1:80'
    'sub.localhost': '127.0.0.1:9012',
  }
}

//
// ...and then pass them in when you create your proxy.
//
var proxyServer = httpProxy.createServer(options).listen(80)

当我 运行 使用节点访问此文件并尝试访问 localhostsub.localhost 时,我返回此错误。我真的不明白怎么了。

Error: Must provide a proper URL as target
    at ProxyServer.<anonymous> (D:\myProjects\bitbucket\temp\node_modules\http-proxy\lib\http-proxy\index.js:68:35)
    at Server.closure (D:\myProjects\bitbucket\temp\node_modules\http-proxy\lib\http-proxy\index.js:125:43)
    at emitTwo (events.js:87:13)
    at Server.emit (events.js:172:7)
    at HTTPParser.parserOnIncoming [as onIncoming] (_http_server.js:525:12)
    at HTTPParser.parserOnHeadersComplete (_http_common.js:88:23)

试试这个:

var http = require("http");
var httpProxy = require("http-proxy");

// reverse proxy
var proxy = httpProxy.createProxyServer();
http.createServer(function (req, res) {
    var target,
        domain = req.headers.host,
        host = domain.split(":")[0];
    ////////////////// change this as per your local setup 
    ////////////////// (or create your own more fancy version! you can use regex, wildcards, whatever...)
    if (host === "localhost") target = {host: "localhost", port: "2000"};
    if (host === "testone.localhost") target = {host: "localhost", port: "3000"};
    if (host === "api.testone.localhost") target = {host: "localhost", port: "4000"};
    //////////////////
    proxy.web(req, res, {
        target: target
    });
}).listen(8000);