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
})
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.
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With