部署生产Node.js服务器 [英] Deploying a production Node.js server

查看:107
本文介绍了部署生产Node.js服务器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经编写了一个Node.js应用程序,我希望在我们的一台生产机器上运行它。这似乎是一个很常见的请求,但我找不到足够的解决方案。是否没有确定部署生产Node.js应用程序的解决方案?



应用程序简单(< 100 LOC),但需要非常高效,可靠,可以运行连续多年,无需重启。它将在一个大型站点上运行,数十个连接/秒。 (该应用程序不被用作Web服务器,它只有一个JSON API)



以下是我考虑过的方法,但我仍然不确定: / p>

使用框架(例如Express)



因为应用程序需要高性能并且非常简单,以框架的形式添加膨胀是我想要避免的。



使用启动服务器nohup



这里的主要问题是异常处理,我们(显然)不希望整个服务器崩溃,因为一个例外。根据我的理解,将整个应用程序包装在一个中尝试{} catch {} 循环不会有帮助,因为JavaScript解释器在异常之后保持不可预测的状态。这是正确的吗?



使用像Forever这样的东西



我已经安装了Forever在我们的FreeBSD机器,这是非常buggy。它最终产生了无法永远杀死的无尽的过程。我必须运行 kill -9 来让我的机器恢复,对于在Forever上运行生产应用程序我不太放心。似乎Upstart(类似的工具,但更通用)不会在FreeBSD上运行。



托管解决方案(例如Heroku,Rackspace,Amazon EC2,等等)



这可能是最简单的解决方案,但我们已经有了其他Web服务器的严重硬件。出于财务考虑,这是没有道理的。



当然,必须有一些确定的解决方案?我错过了一些东西吗?

解决方案


  • 你真的应该使用一个框架因为它是经过测试),除非你想要自己处理会话,cookie,中间件等。 Express真的很轻。

  • 使用nohup启动服务器:不应该这样做,只需使用常规的node命令启动它。 Express也将路由包裹在try-catch中,这样您的服务器不会在路由中崩溃。但是,如果您的服务器确实有严重问题,您不应该担心重新启动它(此外,如果至少有2-3个进程,只有一个将会死亡,所以至少有1-2个用户赢得'感觉到一件事情)。

  • 为了监控我个人更喜欢在操作系统级别更多的东西,例如 Upstart Monit

  • 托管解决方案:既然你已经有你自己的严重的硬件,没有必要投资于别的东西。只需使用负载平衡器(也许nginx或node-http-proxy)代理东西。


I've written a Node.js app, I'm looking to get it running on one of our production machines. This seems like a pretty common request yet I can't find an adequate solution. Is there not established solutions for deploying production Node.js apps?

The app is simple (<100 LOC), but needs to be very efficient, reliable and could run continuously for years without restarting. It's going to be run on a large site, with dozens of connections/second. (the app is not used as a webserver, it only has a JSON API)

Here are the approaches I've considered but I'm still not sure about:

Using a framework (eg. Express)

Because the app needs to be high performance and is so simple, adding bloat in the form of a framework is something I want to avoid.

Starting the server with nohup

The main problem here is with exception handling, we (obviously) don't want the entire server to crash because of an exception. From what I understand, wrapping the entire app in a try {} catch {} loop won't help because the Javascript interpreter is left in an unpredictable state after an exception. Is that correct?

Using something like Forever

I've installed Forever in a FreeBSD machine of ours and it was very buggy. It ended up spawning endless processes that couldn't be killed from Forever. I had to run kill -9 to get my machine back and I don't feel too confident about running a production app on Forever. It also seems that Upstart (similar tool, but more generic) won't run on FreeBSD.

Hosted solutions (eg. Heroku, Rackspace, Amazon EC2, etc.)

This is probably the simplest solution, but we already have a the serious hardware for the rest of our webservers. For financial considerations, it doesn't make sense.

Surely there must be some established solution to this? Am I missing something?

解决方案

  • You should really really use a framework (I recommend something like Express since it was battle-tested) unless you want to deal with sessions, cookies, middleware etc by yourself. Express is really light.
  • Starting the server with nohup: you shouldn't do that, just start it with the regular "node" command. Also Express wraps the routes in a try-catch, so your server won't crash in a route. However if your server does have a serious problem, you shouldn't fear restarting it (besides, if you have 2-3 processes at least, only one will die, so there will be at least 1-2 remaining and the user won't feel a thing).
  • For monitoring I personally prefer something more at the OS-level such as Upstart and Monit.
  • Hosting solution: since you already have your own serious hardware stuff, no need to invest money in something else. Just use a load-balancer (maybe nginx or node-http-proxy) to proxy stuff.

这篇关于部署生产Node.js服务器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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