Skip to content

Instantly share code, notes, and snippets.

@stevekinney
Last active August 29, 2015 14:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save stevekinney/bc30f22d7077bae8acdc to your computer and use it in GitHub Desktop.
Save stevekinney/bc30f22d7077bae8acdc to your computer and use it in GitHub Desktop.
JavaScript Outside of the Browser (Draft)

JavaScript Outside of the Browser

What is Node?

Node.js is a server-side platform built on Chrome's V8 JavaScript runtime. It uses non-blocking I/O and asynchronous events. It's lightweight, efficient, and commonly used for real-time web applications.

History

Node.js was released by Ryan Dahl in 2009. Ryan didn't set out to create a server-side JavaScript implementation. It was the asynchronous, non-blocking part that he was interested in. Ryan originally tried to accomplish this using C, Lua, and Haskell. Ryan turned his attention to JavaScript when Google open-sourced its V8 JavaScript engine.

Getting Started

Installing Node

The easiest way to install Node is to Head over to the official website and download the installer. Alternatively, you can use Homebrew or nvm.

Installing Node should automatically install npm as well. You can test to see if it's installed by typing which npm into the terminal.

The Node REPL

Node comes with a REPL built-in. Unlike Ruby, this isn't a separate binary like irb. You can begin the Node REPL by typing node at the command line.

Try the following:

  • 2 + 2;
  • console.log('Hello');
  • var x = 42;
  • x
  • var hello = function () { return 'hello' };
  • console.log(hello());

Press ^C twice to exit the REPL.

Running a Node Program from the Command Line

Create a new file called sample.js with the following content:

var hello = function(){
  return "hello, world";
}

console.log(hello());

Them run it from the terminal:

node sample.js

Modules and CommonJS

Node has a simple module system based on the CommonJS module specification. Node files and modules have a one-to-one correspondence.

To require a module, foo.js, you would do the following:

var foo = require('./foo.js');

The file extension is optional and the variable name is arbitrary. You can call it anything you'd like, but you must store it in a variable. The following is also valid:

var bar = require('./foo');

You must explicitly include a module in every file you use it in. Requiring a module in a top-level file will not make it available throughout your application.

Requiring Built-in Modules

Node has an intentionally small standard library. The standard library is documented in the Node API Documentation.

The standard library can be required from any Node.js program. You do not have to give a relative path. To use the HTTP library, require it as follows:

var http = require('http');

Using Events and EventEmitter

You can also require specific parts of a module.

EventEmitter is a class that is used throughout Node (e.g. in the http module) and many third-party libraries and allows Node applications to respond to events that happen during the lifetime of the application. A server would want to respond requests or connections. In this case, a connection to the server would be considered an event.

var EventEmitter = require('events').EventEmitter;

The EventEmitter class allows you to create an object that can listen for and respond to events.

Create a file named events.js with the following content:

var EventEmitter = require('events').EventEmitter;

var server = new EventEmitter();

server.on('connect', function () {
  console.log('A client has connected.');
});

server.on('disconnect', function () {
  console.log('A client has disconnected.');
});

server.emit('connect');
server.emit('disconnect');

Run the file using node events.js.

A Simple HTTP Server

The built-in http.Server module inherits from EventEmitter.

var http = require("http");

var server = http.createServer().listen(3000, 'localhost');

server.on('request', function(request, response) {
  response.writeHead(200, {"Content-Type": "text/plain"});
  response.write("Hello World");
  response.end();
});

When the server receives a request, a "request" event is emitted and the server responds with a simple HTTP response.

Creating Modules

Creating modules in Node is simple, but you must explicitly export any functionality that you want to make available.

Create two files: foo.js and bar.js.

bar.js should have the following content:

exports.addTwo = function (addend) {
  return addend + 2;
};

var addThree = function (addend) {
  return addend + 3;
};

foo.js should have the following content:

var bar = require('./bar');

console.log(bar.addTwo(2)); // Logs 4.

// Since we only exported `addTwo`, attempting to use `addThree` will throw an exception.

console.log(bar.addThree(2)); // TypeError: Object #<Object> has no method 'addThree'

If you are only exporting one function or object, you can use module.exports instead of defining properties on the exports object.

Replace the contents of bar.js with the following:

module.exports = function (addend) {
  return addend + 2;
};

Replace the contents of foo.js with the following:

var addTwo = require('./bar');

console.log(addTwo(2)); // Logs 4.

package.json

package.json is a manifest that contains information about your Node application/module and its dependencies. You can create a package.json file by hand or you can let npm guide you through the process using the following command:

npm init

npm will ask you a series of questions about your application.

package.json can contain any data you want, but certain properties have particular meanings in Node.

Here is an example of a package.json manifest.

{
  "name": "example-application",
  "version": "0.0.0",
  "description": "An example Node.js application.",
  "main": "index.js",
  "scripts": {
    "test": "mocha ./test"
  },
  "author": "Jumpstart Lab",
  "license": "MIT"
}

For more information on package.json, please refer to Nodejitsu's package.json cheatsheet.

npm

npm is the package manager for node.

npm creates a node_modules folder in your current directory and downloads modules and their dependencies into that folder.

Modules in your node_modules folder can be required without using relative paths.

From the command line, type the following:

npm install request

This will install the request module and all of its dependencies into your local node_modules folder.

The following command will record the dependency in your package.json:

npm install request --save

The example package.json from above should now look as follows:

{
  "name": "example",
  "version": "0.0.0",
  "description": "An example Node application.",
  "main": "index.js",
  "scripts": {
    "test": "mocha ./test"
  },
  "author": "Jumpstart Lab",
  "license": "MIT",
  "dependencies": {
    "request": "^2.40.0"
  }
}

The node_modules directory is typically not checking into version control (by way of .gitignore). This means that you when you clone a repository, you won't get the dependencies along with it.

The following command scans package.json for any dependencies and installs them.

npm install

Sometimes you want a library in development that you don't necessarily need in production. In this case, you would use the following command:

npm install jasmine-node --save-dev

This will install the module and add it to your package.json as a devDependency. The example package.json file now looks like this:

{
  "name": "example",
  "version": "0.0.0",
  "description": "An example Node application.",
  "main": "index.js",
  "scripts": {
    "test": "mocha ./test"
  },
  "author": "Jumpstart Lab",
  "license": "MIT",
  "dependencies": {
    "request": "^2.40.0"
  },
  "devDependencies": {
    "jasmine-node": "^1.14.5"
  }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment