Node.js

Definition

Node is a great tool for local development, and can be a great production webserver. If you want to use it for local development as a webserver, be sure to checkout Express.

Read this to get an overview of Node.

Node Version Switching with nvm

check to see what version you are using by doing:
$ nvm current

To show the actual path, do:
$ nvm which current

Then your path will be similar to:
/Users/a6001374/.nvm/versions/node/v5.6.0/bin/node

List Local and Remote Versions

$ nvm ls
$ nvm ls-remote

Installing a Version

Check to see what versions are available.
$ nvm ls-remote

Install the one you want. For example, to install the latest stable version do:

$ nvm install node
    or for a specific version do:
$ nvm install 6.0.0

Then alias ‘default’ to the new version. This will set a default Node version to be used in any new shell.
$ nvm alias default 6.0.0

Switching Versions

You can install and switch to different versions using the command:

$ nvm use 4.2.1
$ nvm alias default 4.2.1

If you are switching to a new (updated) version of Node, then be aware all of your globally installed packages will be gone since they are tied to specific versions.

Uninstalling a Version

$ nvm uninstall 4.2.1

Updating npm

npm install npm@latest -g

Express Server

Debugging Middleware

When writing middleware function for you routes you may find this debugging code usefull.

/**
 * This can be used during development to inspect the values of req and res. To use, place this
 * middleware piece in the app.post flow below. Example:
 *
 *    app.get('/site/store-locator/:zipCode?',
 *      firstMiddlewareFunction,
 *      debugMiddleware('after firstMiddlewareFunction'),
 *      secondMiddlewareFunction,
 *      render
 *    );
 *
 *
 * @param {object} req Express Request class
 * @param {object} res Express Response class
 * @param {function} next 
 */
function debugMiddleware (commentOrLocation) {
	return function (req, res, next) {
		console.log('\n\n' + commentOrLocation);
		// console.log(res.body);
		next();
	};
}