Hello World HTTP server
First, ensure that you have Node.js installed on your platform. We use https://nvm.sh In this example, we&
Node.js can be used to create command line utilities. The example below reads the first argument from the command line and prints a Hello message.
To run this code on a Unix System:
chmod +x FILE_NAME
./APP_NAME Dragon
On Windows, you do step 1 and run it with node APP_NAME Dragon
#!/usr/bn/env node
'use strict'
/*
The command line arguments are stored in the `process.argv` array,
which has the following structure:
[0] The path of the executable that started the Node.js process
[1] The path to this application
[2-n] the command line arguments
Example: [ '/bin/node', '/path/to/yourscript', 'arg1', 'arg2', ... ]
src: https://nodejs.org/api/process.html#process_process_argv
*/
// Store the first argument as username.
var username = process.argv[2];
// Check if the username hasn't been provided.
if (!username) {
// Extract the filename
var appName = process.argv[1].split(require('path').sep).pop();
// Give the user an example on how to use the app.
console.error('Missing argument! Example: %s YOUR_NAME', appName);
// Exit the app (success: 0, error: 1).
// An error will stop the execution chain. For example:
// ./app.js && ls -> won't execute ls
// ./app.js Dragon && ls -> will execute ls
process.exit(1);
}
// Print the message to the console.
console.log('Hello %s!', username);