我应该如何在同一系统上组织多个 Express 服务器? [英] How should I organize multiple Express servers on the same system?

查看:38
本文介绍了我应该如何在同一系统上组织多个 Express 服务器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用一台服务器来托管多个 Node.js Web 应用程序,这些应用程序分布在多个域中.我目前的做法是在不同的端口上为每个应用程序运行一个 Express 服务器,并运行一个简单地将请求路由(重定向)到正确端口/Express 服务器的基本服务器.这有效,但这意味着我的基本服务器正在路由每个 HTTP 请求(并通过手动重定向它),并且我的用户看到我的应用程序托管在 [hostname.com]:8000.

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 different port, and to run a base server that simply routes (redirects) requests to the correct port/Express server. This works, but it means that my base server is routing every single HTTP request (and by manually redirecting it), and that my users see my apps as hosted at [hostname.com]:8000.

经过一番研究,我发现我可以使用 http-proxy 满足我的路由需求,但我仍然想知道是否有在同一系统上运行多个 Express 服务器的最佳实践.以下是我打算如何做:

After a bit of research, I've found that I can use http-proxy for my routing needs, but I'd still like to know if there's a best practice for running multiple Express servers on the same system. Here's how I'm planning on doing it:

每个 Web 应用程序都有自己的文件夹,具有完整的 Express 文件夹结构(app.js、路由、视图等).应用程序将按域分组,因此文件夹结构示例如下:

Each web app will have its own folder, with a complete Express folder structure (app.js, routes, views, etc.) Apps will be grouped by domain, so an example folder structure would be:

    hostname.com/
        app.js
        routes/
        views/
        ...
        app1/
            app1.js
            routes/
            views/
            ...
        app2
        ...
    hostname2.com/
        app.js
        routes/
        views/
        ...

我必须使用 node 单独运行每个 app.js(或使用 forever,我'm 当前使用),并且每个都必须在内部使用不同的端口,跨应用重定向指向目标应用的端口.

I'll have to run each app.js separately with node (or with forever, which I'm currently using), and each one will have to use a different port internally, with cross-app redirects being pointed at the port of the target app.

所以,这就是我目前的计划.它有什么问题,我应该尽量避免哪些陷阱?最重要的是,对于这个问题,是否有一个既定的解决方案——使用 Node.js/Express 在同一系统上托管多个 Web 应用程序的问题?

So, that's my current plan. What are the problems with it, and what pitfalls should I try to avoid? Most importantly, is there an established solution to this problem - the problem of hosting multiple web apps on the same system with Node.js/Express?

我确实计划最终使用 WebSockets 和 HTTPS,并且我的设置可以支持的带宽量对我来说并不重要 - 这是一个开发服务器(至少现在是这样).感谢 David Ellis 提出 WebSockets 的问题.

I do plan to eventually use WebSockets and HTTPS, and the amount of bandwidth my setup can support is of little importance to me - this is a development server (at least for now). Thanks to David Ellis for bringing up the issue of WebSockets.

第二次感谢 EhevuTov 和 David Ellis 的回答,他们都提供了很大帮助.我仍在确定我的应用程序的整体结构,看起来 这个 StackOverflow 问题

SECOND Thanks to both EhevuTov and David Ellis for their answers, both of which helped greatly. I'm still settling on an overall structure for my application, and it looks like that question is addressed in some detail by this StackOverflow question

第三次自从发布这个问题以来,我已经走了很长一段路(尽管我还有很长的路要走).查看 我 GitHub 存储库中的这个文件,它利用了我的从这个问题的答案中学到了!

THIRD I've come a ways since posting this question (though I have much further to go). Check out this file in my GitHub repository, which leverages what I learned from the answers to this question!

推荐答案

由于 Express 使用 Connect,我'我很确定您可以使用 Connect 的虚拟主机中间件.它的操作类似于其他产品上的其他 vhost 模块.我没有多个域来测试并向您展示正确的代码,但我认为它是这样的:

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)

如果您发现一台 Express 服务器不够用,请考虑使用 API 中的 Node.Cluster.如果这还不够,那么目前的做法是在您的 Express 服务器前面放置一个 asnyc 反向代理,例如 Nginx,并将代理指向您的 Express 服务器.

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.

这篇关于我应该如何在同一系统上组织多个 Express 服务器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆