paulgorman.org/technical

nodejs

Node or node.js or nodejs is a general purpose programming platform for JavaScript, often used for server-side web development. Node used Google’s V8 JavaScript engine and some components written in C; its performance is good.

On Debian:

# apt-get install nodejs npm

npm is the node package manager.

Node has a REPL, invoked with:

$ nodejs
> .help
.break	Sometimes you get stuck, this gets you out
.clear	Alias for .break
.exit	Exit the repl
.help	Show repl options
.load	Load JS from a file into the REPL session
.save	Save all evaluated commands in this REPL session to a file
>

A simple nodejs server:

var http = require('http');
http.createServer(function (req, res) {
    res.writeHead(200, {'Content-Type': 'text/plain'});
    res.end('Hello, World!\n');
}).listen(9090, "127.0.0.1");
console.log('Server running at http://127.0.0.1:9090/');

Run it like:

$ nodejs hello.js

For web apps, many sources recommend using the “express” module to provide basic MVC glue.

$ npm install express

By default, npm install modules to $PWD/node_modules.

Express does not include a template engine. Try Jade. http://jade-lang.com/

npm installs node modules at $NODE_PATH or ~/node_modules/ by default.

var express = require('express');
var app = express.createServer();
app.get('/', function(req, res) {
    res.send('Welcome to node express.');
});
app.listen(9090);