Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to crash a Node.js app programmatically (for test)

Tags:

node.js

I want to test a Node.js app crash, need the process stop, not just throw some errors.

Is there a simple way to do it programmatically?

app.get('/crash', function() {
  //do something to crash it 
})
like image 294
ZHAO Xudong Avatar asked Sep 23 '14 07:09

ZHAO Xudong


People also ask

How do I terminate a program in node JS?

Method 1: Using ctrl+C key: When running a program of NodeJS in the console, you can close it with ctrl+C directly from the console with changing the code shown below: Method 2: Using process. exit() Function: This function tells Node. js to end the process which is running at the same time with an exit code.

How do I exit a node script?

exitCode = 1; and when the program ends, Node. js will return that exit code. A program will gracefully exit when all the processing is done.


2 Answers

Try this:

app.get('/crash', function() {
  process.nextTick(function () {
    throw new Error;
  });
})

process.nextTick is required to make error asynchronous, otherwise Express will catch it.

like image 176
vkurchatkin Avatar answered Oct 06 '22 20:10

vkurchatkin


process.exit(1)

http://nodejs.org/api/process.html#process_process_exit_code

process.kill(process.pid)

http://nodejs.org/api/process.html#process_process_kill_pid_signal

Both will exit the process.

like image 39
matys84pl Avatar answered Oct 06 '22 20:10

matys84pl