最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

是否有办法在503错误后自动重新启动nodejs应用?

运维笔记admin15浏览0评论

是否有办法在503错误后自动重新启动nodejs应用?

是否有办法在503错误后自动重新启动nodejs应用?

我的nodejs应用程序运行后每隔几周不断获得503资源超出错误,因此我需要通过ssh重新启动它。我想知道是否可以安装一些东西,以便在出现错误或崩溃时自动重新启动。

我在错误期间检查了我们的A2Hosting服务器的进程数,但它只显示0/50。

我正在实时使用推送器。

const express = require("express");
const router = express.Router();

const Pusher = require("pusher");

var pusher = new Pusher({
  appId: "xxxxxx",
  key: "xxxxxxxxxxxxxxxxxx",
  secret: "xxxxxxxxxxxxxxx",
  cluster: "ap1",
  encrypted: true
});

router.post("/", (req, res) => {
  const newVote = {
    id: req.body.id,
    points: 1
  };

  pusher.trigger("scan", "scan-player", {
    id: req.body.id,
    player_id: req.body.player_id,
    admin_id: req.body.admin_id
  });

  return res.json({
    success: true,
    message: "Scan successful!",
    id: req.body.id
   });
});

module.exports = router;
回答如下:

错误不应使您的应用崩溃,您应该处理它并仅记录错误消息。例如,对于express error handler,请参见:error handling patterns with express

此外,如果您的应用崩溃,某些程序可以自动将其重新启动。最基本的是forever

forever start app.js

崩溃时会重新启动您的应用。但是我认为最好的是pm2

pm2 start app.js

它具有相同功能,但是具有许多其他功能,例如负载平衡,部署系统,日志管理

希望有帮助,最好的问候

发布评论

评论列表(0)

  1. 暂无评论