Simple Ways How to To Run and Exit Node.js - AkuCode
Run Node.js scripts from the command line
How to run any Node.js script from the CLI. The unusual way to run a node program is to call the node globally available command (once you install Node) and pass the name of the file you want to execute. If your main Node application file is in app.js, you can call it by typing:
node app.js
How to exit from a Node.js program
Learn how to terminate a Node.js app in the best possible way. There are various ways to terminate a Node.js application. When running a program in the console you can close it with ctrl+C, but what I want to discuss here is programmatically exiting.
Let’s start with the most drastic one, and see why you’re better off not using it. The process core modules provide a handy method that allows you to programmatically exit from a Node.js program: process.exit(). When Node.js runs this line, the process is immediately forced to terminate. This means that any callback that’s pending, any network request still being sent, any filesystem access, or processed writing to stdout or stderr – all is going to be ungracefully terminated right away. If this is fine for you, you can pass an integer that signals the operating system the exit code:
process.exit(1)
By default the exit code is 0, which means success, different exit codes have different meanings, which you might want to use in your own system to have the program communicate to other programs. You can read more on exit codes at the official. You can also set the process.exitCode property:
process.exitCode = 1
And when the program will later end, Node will return that exit code. A program will gracefully exit when all the processing is done. Many times with Node we start server, like this HTTP server:
const express = require('express')
const app = express()
app.get('/',(req,res) => {
res.send('Hi!')
})
app listen(3000,() =>
console.log('Server Ready))
This program is never going to end. If you call process.exit(), an currently pending or running request is going to be aborted. This not nice in this case you need to send the command a SIGTERM signal, and handle that with the process signal handler :
Note: process does not require a “require”, it’s automically available
const express = require('express')
const app = express()
app.get('/',(req,res) => {
res.send('Hi!')
})
app listen(3000,() =>
console.log('Server Ready))
process.on('SIGTEM', () => {
server.close(() => {
console.log('Process Terminated')
})
})
Note: what are signals? Signals are a POSIX intercommunication system: a notification sent to a process in order to notify it of an event that occurred.
SIGKILL is the signals that tells a process to gracefully terminate. It's the signal that’s sent from process managers like upstart or supervisord and many other. You can send this signal from inside the program, in another function :
process.kill(process.pid, ‘SIGTERM’)
Or from another Node.js running program, or any other app running in your system that knows the PID of the process you want to terminate.
How to read environment variables
Learn how to read and make use of environment variables in a Node.js program. The process core module of Node provides the env property which hosts all the environment variables that were set at the moment the process was started. Here is an example that accesses the NODE_ENV environment variable, which is set to development
by default.
Note: process does not require a “require”. It’s automatically available
process.env.NODE_ENV //” development “
Setting it to “production” before the script runs will tell Node that this is a production anvironment. In the same way you can access any custom environment variable you set.
Comments