ฉันจะใช้ Forever ด้วย Express เพื่อให้เซิร์ฟเวอร์ NodeJS ทำงานได้อย่างไร

ฉันมีเซิร์ฟเวอร์ Express NodeJS ที่ฉันเริ่มต้นด้วยตนเองผ่านเทอร์มินัลด้วย npm start ในโฟลเดอร์รูทของโปรเจ็กต์ ฉันดาวน์โหลดและติดตั้งแพ็คเกจ Forever ทั่วโลก เมื่อฉันรัน Forever กับไฟล์ app.js ของฉันโดยใช้:

forever start app.js

เซิร์ฟเวอร์ของฉันไม่เริ่มทำงาน ฉันถือว่านี่เป็นเพราะไม่มีคำสั่ง createServer ที่ชัดเจนในไฟล์ app.js ฉันควรรันไฟล์ใดกับคำสั่ง forever start เพื่อเริ่มต้นเซิร์ฟเวอร์ของฉัน


person Lloyd Banks    schedule 01.08.2014    source แหล่งที่มา


คำตอบ (4)


บนเซิร์ฟเวอร์โหนดของฉัน ฉันใช้ npm forever โดย:

sudo forever start app.js

สังเกตว่าคุณต้อง sudo มัน

person Sterling Archer    schedule 01.08.2014
comment
ฉันได้ลองใช้ sudo แล้ว แต่เซิร์ฟเวอร์ยังคงไม่สามารถใช้งานได้ ฉันไม่ได้รับข้อผิดพลาดเทอร์มินัล ดังนั้นดูเหมือนว่าคำสั่งได้ดำเนินการไปแล้ว แต่ไม่ได้อยู่ในไฟล์ที่ถูกต้อง ถ้าฉันเริ่มต้นด้วย npm start ฉันสามารถไปถึงจุดสิ้นสุดที่ฉันตั้งค่าไว้ได้ แต่ด้วย forever start จุดสิ้นสุดจะหยุดทำงาน - person Lloyd Banks; 01.08.2014
comment
แอพของคุณฟังอยู่หรือเปล่า? - person Sterling Archer; 01.08.2014

สิ่งที่คุณต้องทำก็แค่เรียกใช้คำสั่ง forever start ./bin/www ในโฟลเดอร์โปรเจ็กต์ของคุณ แล้วทุกอย่างจะเรียบร้อย :)

person Danilo    schedule 15.12.2015

ก่อนอื่น ฉันสร้างสคริปต์ Upstart ฉันใช้งาน Amazon EC2 AMI แต่มีเครื่องมืออื่นๆ เช่นนี้สำหรับ OS อื่นๆ

# 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

จากนั้นฉันก็ใช้ไฟล์ start.js ที่ "โฮสต์" แอปของฉัน แอปจริงของฉันอยู่ใน index.js คุณสามารถข้าม 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 );
} );

ได้ผลสำหรับฉัน! คุณสามารถข้ามขั้นตอนเริ่มต้นได้หากต้องการให้แอปของคุณทำงานต่อไป นี่คือโซลูชันที่ใช้งานจริงของฉัน

person clay    schedule 04.08.2014

forever -w ./bin/www 

ในโฟลเดอร์โปรเจ็กต์ของคุณ ให้รันคำสั่ง forever -w ./bin/www มันได้ผลสำหรับฉัน ฉันแน่ใจว่ามันจะได้ผลสำหรับคุณ

person Gourab Sarkar    schedule 04.04.2016