简体   繁体   English

如何使用Express永远保持NodeJS服务器运行?

[英]How do I Use Forever With Express to Keep a NodeJS Server Running?

I have an Express NodeJS server that I manually start through terminal with npm start in my project root folder. 我有一个Express NodeJS服务器,我在我的项目根文件夹中通过npm start手动启动终端。 I downloaded and installed the Forever package globally. 我全局下载并安装了Forever软件包。 When I run Forever against my app.js file using: 当我使用以下命令对我的app.js文件运行Forever时:

forever start app.js

my server doesn't start. 我的服务器无法启动。 I am assuming this is because there is no explicit createServer command in the app.js file. 我假设这是因为app.js文件中没有显式的createServer命令。 What file should I run against the forever start command to start my server? 我应该针对forever start命令运行什么文件来启动我的服务器?

On my node server, I use npm forever by: 在我的节点服务器上,我npm forever使用npm forever

sudo forever start app.js

Notice you need to sudo it 注意你需要sudo

您只需要在项目文件夹中运行命令forever start ./bin/www ,一切都会好的:)

First, I create an Upstart script. 首先,我创建一个Upstart脚本。 I'm running on Amazon EC2 AMI, but there are other tools like this for other OSes. 我在Amazon EC2 AMI上运行,但是对于其他操作系统,还有其他类似的工具。

# This is an upstart (http://upstart.ubuntu.com/) script
# to run the node.js server on system boot and make it
# manageable with commands such as
# 'start app' and 'stop app'
#
# This script is to be placed in /etc/init to work with upstart.
#
# Internally the 'initctl' command is used to manage:
# initctl help
# initctl status node-app
# initctl reload node-app
# initctl start node-app

description "node.js forever server for app"

#node child process might not really fork, so don't except it
#expect fork

# used to be: start on startup
# until we found some mounts weren't ready yet while booting:

start on runlevel [2345]
stop on runlevel [016]

# Automatically Respawn:
respawn
respawn limit 99 5

chdir /path/to/directory/node-app

exec node start.js

#post-start script
#   # Optionally put a script here that will notifiy you node has (re)started
#   # /root/bin/hoptoad.sh "node.js has started!"
#end script

Then I used a start.js file that "hosts" my app. 然后我使用了一个“托管”我的应用程序的start.js文件。 My real app is located in index.js . 我的真实应用程序位于index.js You could skip the process.on stuff at the bottom, but I like it there. 你可以跳过底部的process.on东西,但我喜欢那里。

/*jslint node: true */
"use strict";

/**
 * File to start using forever, logs crashes, restarts on file changes, etc.
 */

var cmd = ( process.env.DBG ? "node --debug" : "node" );

var forever = require( 'forever' ),
  //exec = require('child_process').exec,
  child = new( forever.Monitor )( 'index.js', {
    'silent': false,
    'pidFile': 'pids/node-app.pid',
    'watch': true,
    'command': cmd,
    //"max" : 10,
    'watchDirectory': './lib', // Top-level directory to watch from.
    'watchIgnoreDotFiles': true, // whether to ignore dot files
    'watchIgnorePatterns': [], // array of glob patterns to ignore, merged with contents of watchDirectory + '/.foreverignore' file
    'logFile': 'logs/forever.log', // Path to log output from forever process (when daemonized)
    //'outFile': 'logs/forever.out', // Path to log output from child stdout
    'errFile': 'logs/forever.err'
  } );

child.on( "exit", function() {
  console.log( 'node-app has exited!' );
} );
child.on( "restart", function() {
  console.log( 'node-app has restarted.' );
} );


child.start();
forever.startServer( child );

process.on( 'SIGINT', function() {
  console.log( "\nGracefully shutting down \'node forever\' from SIGINT (Ctrl-C)" );
  // some other closing procedures go here
  process.exit();
} );

process.on( 'exit', function() {
  console.log( 'About to exit \'node forever\' process.' );
} );

process.on( 'uncaughtException', function( err ) {
  console.log( 'Caught exception in \'node forever\': ' + err );
} );

Works for me! 适合我! You can skip the upstart stuff if you just want to see your app keep running - this is my production solution. 如果你只是想看到你的应用程序继续运行,你可以跳过新手的东西 - 这是我的生产解决方案。

forever -w ./bin/www 

In your project folder, run the command forever -w ./bin/www . 在项目文件夹中, forever -w ./bin/www运行命令forever -w ./bin/www It worked for me. 它对我有用。 I am sure it will work for you. 我相信它会对你有用。

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

相关问题 如何在运行Express JS服务器时永久使用 - How to use forever while running the express js server 如何使用 Forever 运行带有 typescript 文件的 NodeJS Express API? - How do you run a NodeJS Express API with typescript files with Forever? NodeJS-让我的应用程序永远运行 - NodeJS - Keep my App running with Forever 即使我使用的是永久性的npm模块,如何使我的web服务在nodejs中保持永久性运行? - How to keep my web services awake in nodejs to run forever though i am using forever npm module? 永远运行的Node.js脚本中的服务器性能 - Server performance in nodejs script running forever 为什么注销后永远无法使nodejs进程运行? - Why is forever not working to keep my nodejs process running after I logout? 为什么在运行 nodejs express 服务器时出现错误:找不到模块 'html'? - Why do I get Error: Cannot find module 'html' when running nodejs express server? 如何让 EC2 上的 Node.js 服务器永远运行? - How do I leave Node.js server on EC2 running forever? 如何在NodeJS / Express服务器上使用Postgres池 - How to use Postgres pooling on NodeJS/Express server nodejs 事件循环如何在没有 for/while 永远循环的情况下继续运行? - How does nodejs event loop keep running without a for/while forever loop?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM