简体   繁体   English

如何防止 Node.js Web 服务器停机

[英]How to prevent downtime on Node.js web server

I have a Node.js web application and a postgres database being hosted on a raspberry pi on my local network.我有一个 Node.js Web 应用程序和一个 postgres 数据库,该数据库托管在本地网络的树莓派上。 They are being frequently accessed by other devices on the local network during the work day, and any system downtime results in the delay or failure of operations.它们在工作日被本地网络上的其他设备频繁访问,任何系统停机都会导致操作延迟或失败。

What are some possible solutions to this issue?这个问题有哪些可能的解决方案? Do I need to deploy the Node.js web application using something like Heroku or is there a way that I could add redundancy by hosting the Node.js web application on multiple different machines to allow the application to keep running in the event of a crash?我是否需要使用 Heroku 之类的东西来部署 Node.js Web 应用程序,或者有没有一种方法可以通过在多台不同的机器上托管 Node.js Web 应用程序来增加冗余,以允许应用程序在发生崩溃时继续运行?

You can use some process manager for node.js like PM2 .您可以为 node.js 使用一些进程管理器,例如PM2 You can setup restart strategies for your nodejs server.您可以为 nodejs 服务器设置重启策略。 When it crush PM2 will restart it.当它粉碎 PM2 将重新启动它。 See this docs .请参阅此文档 Also it can running you nodejs app in cluster mode - you can have separate instances of you app and PM2 will distribute all request between them.它也可以在集群模式下运行你的 nodejs 应用程序——你可以有你的应用程序的单独实例,PM2 将在它们之间分发所有请求。
Also if you nodejs process crash because some errors happen, try to process this errors in right way, without finish process.此外,如果您的 nodejs 进程因发生某些错误而崩溃,请尝试以正确的方式处理此错误,而不要完成进程。 Add try catch when it necessary, handle async errors, etc..必要时添加 try catch、处理异步错误等。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM