Things have been slow on the blogging front but there has been progress on the Zite replacement. I’ll write more about that soon but part of what I have been doing is looking into what server-side technology to use.
As far as a database goes, this seems like a no-brainer. I’m dealing with JSON documents that I can either spend some effort on normalizing to put into a SQL database, or simply keep them as is and put them in a database that supports that form, and the obvious choice then is MongoDB, which uses a binary form of JSON.
- require() method used to load a module
- exports object to expose code
- module object
Each module is a single JS file with isolated scope overridden by the exports object. E.g. consider a file hello.js:
which can then be used like:
Alternatively we could have written:
Node core modules
Core modules are compiled into the node binary, and include ‘fs’ for file system access, and ‘http’ for network support. They still need to be loaded with require(). See http://nodejs.org/api/ for more about the core modules.
npm - The node package manager
npm can install packages and their dependencies locally (in ./node_modules) or globally with the -g option (in /usr/local/lib/node_modules). You would typically install common build tools like grunt globally, and dependencies for a project locally.
Note that dependencies are not installed at the top-level but within the package directory. So if you do ’npm install express’ then express’s dependencies will be installed in ./node_modules/express/node_modules directory.
You can specify package@version if you need a specific version. See https://github/com/isaacs/node-semver for details on semantic versioning names.
Use ’npm uninstall’ to remove a package (include -g for global packages), and ’npm update’ to update a package.
For multiple packages use a package.json file, with (at least) “name”, “version”
and “dependencies” entries. Use “npm init” to create a skeleton package.json.
Then use “npm install”/“npm update” to install/update all dependencies specified
in the file. You can use “npm install
To use modules, use require(). If you just specify a name, node will first look in the core modules folder, then the node_modules/module folder. Alternatively you can specify an absolute or relative path (the .js extension is not needed). You can load an entire folder with a single require() if that folder has a package.json file (the “main” module will be returned from require()).
The Connect module
The Connect module builds on top of the http module, and allows the chaining of request handlers (e.g. for logging, static file handling, etc). Each handler has three arguments:
- req - the request object
- res - the response object
- next - the next handler in the chain
Here is a simple server:
If this is in a file server.js, we can run it with ’node server’ and access it at http://localhost:8000
Express web framework
Express is a node web framework built on top of Connect. Here is the same server as before written using Express:
Request objects have methods to retrieve parameters, POST bodies, cookies, etc, while the response objects have methods to set the status code, cookies, headers, and do redirects.