Node.js Installation and Setup

Install and Setup Node

Install NVM (Node Version Manager)

Follow the install instructions for NVM:
https://github.com/creationix/nvm

In Terminal run the online install script and download (clone from GitHub) the NVM repository to your users directory ~/.nvm:

curl -o- https://raw.githubusercontent.com/creationix/nvm/v0.33.1/install.sh | bash

After the download is complete load NVM:

export NVM_DIR="$HOME/.nvm"
[ -s "$NVM_DIR/nvm.sh" ] && . "$NVM_DIR/nvm.sh" # This loads nvm

Verify that NVM has been installed:

command -v nvm; nvm --version`

Find out more about NVM management.

Find the current stable Node Version

Find the latest LTS (long-term support) version Node at the website:
https://nodejs.org/en/

Install Node

The latest stable version of Node is recommend for production. Stable versions have LTS (Long-Term Support) via security and bug patches. Stable versions of Node start with even numbers (4, 6, 8 …) and the experimental versions start with odd numbers (5, 7 …).

In Terminal install the latest stable version of Node:

nvm install 6.10.1

You can also install the latest unstable version:

nvm install node

After installing, check the version:

node --version

Switch to the stable version:

nvm use 6.10.1

Check which version of Node is now running:

node --version

Running Node

Start Node

In Terminal start Node.js by typing:

node

Test Node

Test-run a started instance of Node by entering a command into Terminal:

console.log('Hello, world!');

You should get the output:

Hello, world!
undefined

Feel free to interface with Node via the command line in this way to test small bits of code in which you don’t want to waste time putting into a file.

Stop Node

In Terminal stop Node.js by typing:

.exit

Apps

Test App

Create a new file on your computer called test.js with this content:

// test.js

console.log('Hello, world!');

To run the file open Terminal and navigate to the directory containing the test.js.

cd /path/to/my/file

Run the file using Node:

node test.js

Basic HTTP-Based App

Create a new file on your computer called test.http.js with this content:

// test.http.js

/**
 * A variable is a data structure that contains information that is expected to change.
 * A constant is a data structure that contains information that will never change.
 * We use constants to define the application objects and vars for data.
 */

// Make a core 'http' module object.
// 'require' loads files and core modules. In Node, each included file is treated as a separate module.
const http = require('http');

// Define the port we want to listen on.
const port = 3000;

// Set a generic response message value.
var responseMessage = 'Thanks for requesting me!';

// Define a function to return the response message.
function getResponseMessage()
{
    return responseMessage;
}

// Define the handler for incoming requests.
const requestHandler = ( request, response ) =>
{
    // Log the request.
    console.log( 'request: ' + request.url );

    // Set the response message by calling our function.
    response.end( getResponseMessage() );
}

// Make the server object.
const server = http.createServer( requestHandler );

/**
 * Start the server
 *
 * Begin accepting connections on the specified port and hostname.
 * If the hostname is omitted, the server will accept connections on the unspecified
 * IPv6 address (::) when IPv6 is available, or the unspecified IPv4 address (0.0.0.0) otherwise.
 */
server.listen(
    port, ( err ) =>
    {
        if( err )
        {
            // Return the error message if something goes wrong.
            return console.log( 'something bad happened', err );
        }

        // log a message to indicate the server has started.
        console.log( 'server is listening on ' + port );
    }
);

In Terminal navigate to the directory containing the test.http.js file and then launch it with Node:

node test.http.js

Now in your web browser visit port 3000 at your localhost http://localhost:3000 to send a request to the Node HTTP app.