How should I organize multiple Express servers on the same system?

前端 未结 2 1486
说谎
说谎 2020-12-07 07:25

I\'m using one server to host multiple Node.js web apps, which are distributed across multiple domains. My current practice is to run an Express server for each app on a dif

相关标签:
2条回答
  • 2020-12-07 08:09

    Since Express uses Connect, I'm pretty sure you can use Connect's virtual host middleware. It operates similar to other vhost modules on other products. I don't have multiple domains to test and show you proper code, but I would think it's something like this:

    express.createServer()
    .use(express.vhost('hostname1.com', require('/path/to/hostname1').app)
    .use(express.vhost('hostname2.com', require('/path/to/hostname2').app)
    .listen(80)
    

    If you get to the point where one Express server isn't enough, then look into using the Node.Cluster from the API. If that also isn't enough, then the current practice is to put a asnyc reverse proxy such as Nginx in front of your Express servers and point the proxies to your Express servers.

    0 讨论(0)
  • 2020-12-07 08:16

    If you don't need to use WebSockets (or any HTTP 1.1 feature, really), you can use NginX as your proxy instead.

    The advantage is the total load NginX can handle versus Node is higher (being statically compiled and specialized for this sort of thing, basically), but you lose the ability to stream any data (sending smaller chunks at a time).

    For a smaller site, or if you're unsure what features you'll need in the future, it's probably better to stick with node-http-proxy and only switch to NginX if you can demonstrate the proxy is the bottleneck on your server. Fortunately NginX isn't hard to set up if you do need it later.

    0 讨论(0)
提交回复
热议问题