Node.js and CouchDB feel like they were made for each other right from the very first time I used them. With the cradle node package the integration becomes even easier.
Whilst both Node.js and CouchDB are open source with packages for most operating systems it maybe easier for you to start out using a hosted solution such as CloudNo.de (has CouchDB now) or Nodester for example. As far as the CouchDB portion goes there is only one place to go and that is IrisCouch.
With the exception of IrisCouch they are all in private beta so there might be a small wait before you get access.
I wrote the following example code before CloudNo.de supported CouchDB so it references IrisCouch, but the setup is similar.
So at this point I am going to assume that you have successfully installed Node.js and CouchDB or are using hosted services. All the projects have good installation documentation for most platforms and there are many blog posts rehashing this step out there.
Firstly you will need to install the cradle package using:
npm install cradle
It should be noted here that if you are using a hosted service the command will be something like:
cloudnode app npm install cradle
from the applications root directory (the one with the .git folder in it). Both the CloudNo.de documentation and Nodester documentation cover this process on their respective websites.
CouchDB is documented on the Apache foundation's web pages and the cradle middleware has its source and documentation on GitHub.
var http = require('http'); http.createServer(function (req, http_res) { http_res.writeHead(200, {'Content-Type': 'text/plain'}); var response = ''; var cradle = require('cradle'); var connection = new(cradle.Connection)('https://subdomain.iriscouch.com', 443, { auth: { username: 'username', password: 'password' } }); var db = connection.database('database_name'); db.save('document_key', { name: 'A Funny Name' }, function (err, res) { if (err) { // Handle error response += ' SAVE ERROR: Could not save record!!\n'; } else { // Handle success response += ' SUCESSFUL SAVE\n'; } db.get('document_key', function (err, doc) { response += ' DOCUMENT: ' + doc + '\n'; http_res.end(response); }); }); }).listen(8071);I think that the code is fairly easy to follow if you have written JavaScript code utilising callbacks before, but I will step through it for clarity.
A new HTTP server is setup first so that we can access the Node.js programme through the a web browser to trigger the database changes. Incidentally this would be accessed at http://localhost:80871 or http://subdomain.cloudno.de.
Cradle is then setup and the connection parameters are set with the CouchDB database being selected on line 11.
Next a new document is saved into the DB (replace document_key with your document name to customise) with the contents of { name: "A funny name" }. Once this is saved a callback function is called that will attempt to retrieve the record.
Depending upon how successful the whole process is you should see something like
SUCESSFUL SAVE DOCUMENT: { name: "A funny name" }in your browser.
As you can see it is really easy to change what is saved and how it is retrieved. This was just a very simple introduction, but there are many neat tricks documented at each projects homepage.
restify is a smallish framework, similar to express for building REST APIs. For full details, see http://mcavage.github.com/node-restify.
UsageServer
var restify = require('restify'); var server = restify.createServer({ name: 'myapp', version: '1.0.0' }); server.use(restify.acceptParser(server.acceptable)); server.use(restify.queryParser()); server.use(restify.bodyParser()); server.get('/echo/:name', function (req, res, next) { res.send(req.params); return next(); }); server.listen(8080, function () { console.log('%s listening at %s', server.name, server.url); });Client
var assert = require('assert'); var restify = require('restify'); var client = restify.createJsonClient({ url: 'http://localhost:8080', version: '~1.0' }); client.get('/echo/mark', function (err, req, res, obj) { assert.ifError(err); console.log('Server returned: %j', obj); });Installation$ npm install restifyLicense
The MIT License (MIT) Copyright (c) 2012 Mark Cavage
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
Bugs
See https://github.com/mcavage/node-restify/issues.
Mailing list
See the Google group .
About restifyInstallationrestify is a node.js module built specifically to enable you to build correct REST web services. It borrows heavily from express (intentionally) as that is more or less the de facto API for writing web applications on top of node.js.
Why use restify and not express?
I get asked this more than anything else, so I'll just get it out of the way up front.
Express' use case is targeted at browser applications, and contains a lot of functionality (i.e., templating/rendering) to support that. Restify does not. Restify exists to let you build "strict" API services that are maintanable and observable; restify comes with automatic DTrace support for all your handlers, if you're running on a platform that supports DTrace.
In short, I wrote restify as I needed a framework that gave me absolute control over interactions with HTTP and full observability into the latency and characteristics of my applications. If you don't need that, or don't care about those aspect(s), then it's probably not for you.
About this guide
This guide provides comprehensive documentation on writing a REST api (server) with restify, writing clients that easily consume REST APIs, and on the DTrace integration present in restify.
Note this documentation refers to the 1.0.X version(s) of restify; these versions are not backwards-compatible with the 0.5 versions.
Conventions
Any content formatted like this:
curl localhost:8080is a command-line example that you can run from a shell. All other examples and information is formatted like this:
GET /foo HTTP/1.1npm install restifyServer APIThe most barebones echo server:
var restify = require('restify'); function respond(req, res, next) { res.send('hello ' + req.params.name); } var server = restify.createServer(); server.get('/hello/:name', respond); server.head('/hello/:name', respond); server.listen(8080, function() { console.log('%s listening at %s', server.name, server.url); });Try hitting that with the following curl commands to get a feel for what restify is going to turn that into:
curl -is http://localhost:8080/hello/mark -H 'accept: text/plain' HTTP/1.1 200 OK Access-Control-Allow-Origin: * Access-Control-Allow-Methods: GET Access-Control-Allow-Headers: Accept, Accept-Version, Content-Length, Content-MD5, Content-Type, Date, X-Api-Version Access-Control-Expose-Headers: X-Api-Version, X-Request-Id, X-Response-Time Server: restify X-Request-Id: b0cec85c-cc17-4985-8f2f-7787ad6e3512 Date: Fri, 30 Dec 2011 20:09:36 GMT X-Response-Time: 1 Content-Type: text/plain Content-Length: 10 Content-MD5: pvc6+Tyy1EBQgBWaN1v+eQ== Connection: close hello mark $ curl -is http://localhost:8080/hello/mark HTTP/1.1 200 OK Access-Control-Allow-Origin: * Access-Control-Allow-Methods: GET Access-Control-Allow-Headers: Accept, Accept-Version, Content-Length, Content-MD5, Content-Type, Date, X-Api-Version Access-Control-Expose-Headers: X-Api-Version, X-Request-Id, X-Response-Time Server: restify X-Request-Id: 07c4b4b5-5866-4b08-ace8-63ef7c70b48f Date: Fri, 30 Dec 2011 20:11:17 GMT X-Response-Time: 1 Content-Type: application/json Content-Length: 12 Content-MD5: UxC9l75x6aHfos8MhdrDLg== Connection: close "hello mark" $ curl -is http://localhost:8080 -X HEAD HTTP/1.1 200 OK Access-Control-Allow-Origin: * Access-Control-Allow-Methods: GET Access-Control-Allow-Headers: Accept, Accept-Version, Content-Length, Content-MD5, Content-Type, Date, X-Api-Version Access-Control-Expose-Headers: X-Api-Version, X-Request-Id, X-Response-Time Server: restify X-Request-Id: 07c4b4b5-5866-4b08-ace8-63ef7c70b48f Date: Fri, 30 Dec 2011 20:11:17 GMT X-Response-Time: 1 Content-Type: application/json Content-Length: 12 Content-MD5: UxC9l75x6aHfos8MhdrDLg== Connection: closeNote that all of those headers are set automatically IFF you don't set them explicitly (or register a defaultResponseHeaders function).
Creating a Server
Creating a server is straightforward, as you simply invoke the createServer API, which takes an options object with the options below (and listen() takes the same arguments as node's http.Server.listen):
var restify = require('restify'); var server = restify.createServer({ certificate: ..., key: ..., name: 'MyApp', }); server.listen(8080);
Option Type Description certificate String If you want to create an HTTPS server, pass in the PEM-encoded certificate and key key String If you want to create an HTTPS server, pass in the PEM-encoded certificate and key formatters Object Custom response formatters for res.send() log Object You can optionally pass in a bunyan instance; not required name String By default, this will be set in the Server response header, and also will name the DTrace provider; default is restify urlParamPattern String Overrides the RegExp used to replace :name parameters. By default, uses [a-zA-Z0-9-_~\\.%]+ (RFC3986 compliant). version String A default version to set for all routes responseTimeHeader String By default, this will be X-Response-Time responseTimeFormatter function Allows you to apply formatting to the value of the header. The duration is passed as an argument in number of milliseconds to execute. example Common handlers: server.use()
A restify server has a use() method on it, which takes handlers of the form function (req, res, next). Note that restify runs handlers in the order they are registered on a server, so if you want some common handlers to run before any of your routes, issue calls to use() before defining routes.
Note that in all calls to use() and the routes below, you can pass in any combination of function(res, res, next) and [function(req, res, next)] (i.e., direct functions and arrays of functions).
Routing
restify routing, in 'basic' mode, is pretty much identical to express/sinatra, in that HTTP verbs are used with a parameterized resource to determine what chain of handlers to run. Values associated with named placeholders are available in req.params. Note that values will be URL-decoded before being passed to you.
function send(req, res, next) { res.send(hello ' + req.params.name); return next(); } server.post('/hello', function create(req, res, next) { res.send(201, Math.random().toString(36).substr(3, 8)); return next(); }); server.put('/hello', send); server.get('/hello/:name', send); server.head('/hello/:name', send); server.del('hello/:name', function rm(req, res, next) { res.send(204); return next(); });You can also pass in a RegExp object and access the capture group with req.params (which will not be interpreted in any way):
server.get(/^\/([a-zA-Z0-9_\.~-]+)\/(.*)/, function(req, res, next) { console.log(req.params[0]); console.log(req.params[1]); res.send(200); return next(); });Here any request like:
curl localhost:8080/foo/my/cats/name/is/gandalfWould result in req.params[0] being foo and req.params[1] being my/cats/name/is/gandalf. Basically, you can do whatever you want.
Versioned Routes
Most REST APIs tend to need versioning, and restify ships with support for semver versioning in an Accept-Version header, the same way you specify NPM version dependencies:
var restify = require('restify'); var server = restify.createServer(); function sendV1(req, res, next) { res.send('hello: ' + req.params.name); return next(); } function sendV2(req, res, next) { res.send({hello: req.params.name}); return next(); } var PATH = '/hello/:name'; server.get({path: PATH, version: '1.1.3'}, sendV1); server.get({path: PATH, version: '2.0.0'}, sendV2); server.listen(8080);Try hitting with:
curl -s localhost:8080/hello/mark "hello: mark" $ curl -s -H 'accept-version: ~1' localhost:8080/hello/mark "hello: mark" $ curl -s -H 'accept-version: ~2' localhost:8080/hello/mark {"hello":"mark"} $ curl -s -H 'accept-version: ~3' localhost:8080/hello/mark | json { "code": "InvalidVersion", "message": "GET /hello/mark supports versions: 1.1.3, 2.0.0" }In the first case, we didn't specify an Accept-Version header at all, so restify treats that like sending a *. Much as not sending an Accept header means the client gets the server's choice. Restify will choose the first matching route, in the order specified in the code. In the second case, we explicitly asked for for V1, which got us the same response, but then we asked for V2 and got back JSON. Finally, we asked for a version that doesn't exist and got an error (notably, we didn't send an Accept header, so we got a JSON response). Which segues us nicely into content negotiation.
You can default the versions on routes by passing in a version field at server creation time. Lastly, you can support multiple versions in the API by using an array:
server.get({path: PATH, version: ['2.0.0', '2.1.0']}, sendV2);Content Negotiation
If you're using res.send() restify will automatically select the content-type to respond with, by finding the first registered formatter defined. Note in the examples above we've not defined any formatters, so we've been leveraging the fact that restify ships with application/json, text/plain and application/octet-stream formatters. You can add additional formatters to restify by passing in a hash of content-type -> parser at server creation time:
var server = restify.createServer({ formatters: { 'application/foo': function formatFoo(req, res, body) { if (body instanceof Error) return body.stack; if (Buffer.isBuffer(body)) return body.toString('base64'); return util.inspect(body); } } });You can do whatever you want, but you probably want to check the type of body to figure out what type it is, notably for Error/Buffer/everything else. You can always add more formatters later by just setting the formatter on server.formatters, but it's probably sane to just do it at construct time. Also, note that if a content-type can't be negotiated, the default is application/octet-stream. Of course, you can always explicitly set the content-type to return by setting it on res.contentType:
res.contentType = 'application/foo'; res.send({hello: 'world'});Lastly, you don't have to use any of this magic, as a restify response object has all the "raw" methods of a node ServerResponse on it as well.
var body = 'hello world'; res.writeHead(200, { 'Content-Length': Buffer.byteLength(body), 'Content-Type': 'text/plain' }); res.write(body); res.end();Error handling
You can handle errors in restify a few different ways. First, you can always just call res.send(err). You can also shorthand this in a route by doing:
server.get('/hello/:name', function(req, res, next) { return database.get(req.params.name, function(err, user) { if (err) return next(err); res.send(user); return next(); }); });If you invoke res.send() with an error that has a statusCode attribute, that will be used, otherwise a default of 500 will be used (unless you're using res.send(4xx, new Error('blah))).
HttpError
Now the obvious question is what that exactly does (in either case). restify tries to be programmer-friendly with errors by exposing all HTTP status codes as a subclass of HttpError. So, for example, you can do this:
server.get('/hello/:name', function(req, res, next) { return next(new restify.ConflictError("I just don't like you")); }); $ curl -is -H 'accept: text/*' localhost:8080/hello/mark HTTP/1.1 409 Conflict Content-Type: text/plain Access-Control-Allow-Origin: * Access-Control-Allow-Methods: GET Access-Control-Allow-Headers: Accept, Accept-Version, Content-Length, Content-MD5, Content-Type, Date, X-Api-Version Access-Control-Expose-Headers: X-Api-Version, X-Request-Id, X-Response-Time Connection: close Content-Length: 21 Content-MD5: up6uNh2ejV/C6JUbLlvsiw== Date: Tue, 03 Jan 2012 00:24:48 GMT Server: restify X-Request-Id: 1685313e-e801-4d90-9537-7ca20a27acfc X-Response-Time: 1 I just don't like youThe core thing to note about an HttpError is that it has a numeric code (statusCode) and a body. The statusCode will automatically set the HTTP response status code, and the body attribute by default will be the message.
All status codes between 400 and 5xx are automatically converted into an HttpError with the name being 'PascalCase' and spaces removed. For the complete list, take a look at the node source.
From that code above 418: I'm a teapot would be ImATeapotError, as an example.
RestError
Now, a common problem with REST APIs and HTTP is that they often end up needing to overload 400 and 409 to mean a bunch of different things. There's no real standard on what to do in these cases, but in general you want machines to be able to (safely) parse these things out, and so restify defines a convention of a RestError. A RestError is a subclass of one of the particular HttpError types, and additionally sets the body attribute to be a JS object with the attributes code and message. For example, here's a built-in RestError:
var server = restify.createServer(); server.get('/hello/:name', function(req, res, next) { return next(new restify.InvalidArgumentError("I just don't like you")); }); $ curl -is localhost:8080/hello/mark | json HTTP/1.1 409 Conflict Content-Type: application/json Access-Control-Allow-Origin: * Access-Control-Allow-Methods: GET Access-Control-Allow-Headers: Accept, Accept-Version, Content-Length, Content-MD5, Content-Type, Date, X-Api-Version Access-Control-Expose-Headers: X-Api-Version, X-Request-Id, X-Response-Time Connection: close Content-Length: 60 Content-MD5: MpEcO5EQFUZ2MNeUB2VaZg== Date: Tue, 03 Jan 2012 00:50:21 GMT Server: restify X-Request-Id: bda456dd-2fe4-478d-809c-7d159d58d579 X-Response-Time: 3 { "code": "InvalidArgument", "message": "I just don't like you" }The built-in restify errors are:
- RestError
- BadDigestError
- BadMethodError
- InternalError
- InvalidArgumentError
- InvalidContentError
- InvalidCredentialsError
- InvalidHeaderError
- InvalidVersionError
- MissingParameterError
- NotAuthorizedError
- RequestExpiredError
- RequestThrottledError
- ResourceNotFoundError
- WrongAcceptError
You can always add your own by subclassing restify.RestError like:
var restify = require('restify'); var util = require('util'); function MyError(message) { restify.RestError.call(this, 418, 'MyError', message, MyError); this.name = 'MyError'; }; util.inherits(MyError, restify.RestError);Basically, a RestError takes a statusCode, a restCode, a message, and a "constructorOpt" so that V8 correctly omits your code from the stack trace (you don't have to do that, but you probably want it). In the example above, we also set the name property so console.log(new MyError()); looks correct.
Server API
Events
Restify servers emit all the events from the node http.Server and has several other events you want to listen on.
Event: 'NotFound'
function (request, response) {}
When a client request is sent for a URL that does not exist, restify will emit this event. Note that restify checks for listeners on this event, and if there are none, responds with a default 404 handler. It is expected that if you listen for this event, you respond to the client.
Event: 'MethodNotAllowed'
function (request, response) {}
When a client request is sent for a URL that does exist, but you have not registered a route for that HTTP verb, restify will emit this event. Note that restify checks for listeners on this event, and if there are none, responds with a default 405 handler. It is expected that if you listen for this event, you respond to the client.
Event: 'VersionNotAllowed'
function (request, response) {}
When a client request is sent for a route that exists, but does not match the version(s) on those routes, restify will emit this event. Note that restify checks for listeners on this event, and if there are none, responds with a default 400 handler. It is expected that if you listen for this event, you respond to the client.
Event: 'after'
function (request, response, route) {}
Emitted after a route has finished all the handlers you registered. You can use this to write audit logs, etc. The route parameter will be the Route object that ran. Note that when you are using the default 404/405/BadVersion handlers, this event will still be fired, but route will be null.
Event: 'uncaughtException'
function (request, response, route, error) {}
Emitted when some handler throws an uncaughtException somewhere in the chain. The default behavior is to just call res.send(error), and let the built-ins in restify handle transforming, but you can override to whatever you want here.
Properties
A restify server has the following properties on it:
Name Type Description name String name of the server version String default version to use in all routes log Object bunyan instance acceptable Array(String) list of content-types this server can respond with url String Once listen() is called, this will be filled in with where the server is running Other Methods
address()
Wraps node's address().
listen(port, [host], [callback]) or listen(path, [callback])
Wrap's node's listen().
close()
Wrap's node's close().
pre()
Allows you to add in handlers that run before routing occurs. This gives you a hook to change request headers and the like if you need to. Note that req.params will be undefined, as that's filled in _after_ routing.
server.pre(function(req, res, next) { req.headers.accept = 'application/json'; // screw you client! return next(); });use()
Allows you to add in handlers that run no matter what the route.
Bundled Plugins
restify ships with several handlers you can use, specifically:
- Accept header parsing
- Authorization header parsing
- Date header parsing
- Query string parsing
- Body parsing (JSON/URL-encoded/multipart form)
- Throttling
- Conditional request handling
- Audit logger
Here's some example code using all the shipped plugins:
var server = restify.createServer(); server.use(restify.acceptParser(server.acceptable)); server.use(restify.authorizationParser()); server.use(restify.dateParser()); server.use(restify.queryParser()); server.use(restify.bodyParser()); server.use(restify.throttle({ burst: 100, rate: 50, ip: true, overrides: { '192.168.1.1': { rate: 0, // unlimited burst: 0 } } })); server.use(restify.conditionalRequest());Accept Parser
Parses out the Accept header, and ensures that the server can respond to what the client asked for. You almost always want to just pass in server.acceptable here, as that's an array of content types the server knows how to respond to (with the formatters you've registered). If the request is for a non-handled type, this plugin will return an error of 406.
server.use(restify.acceptParser(server.acceptable));Authorization Parser
server.use(restify.authorizationParser());Parses out the Authorization header as best restify can. Currently only HTTP Basic Auth and HTTP Signature schemes are supported. When this is used, req.authorization will be set to something like:
{ scheme: <Basic|Signature|...>, credentials: <Undecoded value of header>, basic: { username: $user password: $password } }req.username will also be set, and defaults to 'anonymous'. If the scheme is unrecognized, the only thing avaiable in req.authorization will be scheme and credentials - it will be up to you to parse out the rest.
Date Parser
server.use(restify.dateParser());Parses out the HTTP Date header (if present) and checks for clock skew (default allowed clock skew is 300s, like Kerberos). You can pass in a number, which is interpreted in seconds, to allow for clock skew.
// Allows clock skew of 1m server.use(restify.dateParser(60));QueryParser
server.use(restify.queryParser());Parses the HTTP query string (i.e., /foo?id=bar&name=mark). If you use this, the parsed content will always be available in req.query, additionally params are merged into req.params. You can disable by passing in mapParams: false in the options object:
server.use(restify.queryParser({ mapParams: false }));BodyParser
Blocks your chain on reading and parsing the HTTP request body. Switches on Content-Type and does the appropriate logic. application/json, application/x-www-form-urlencoded and multipart/form-data are currently supported.
server.use(restify.bodyParser({ mapParams: false }));You can pass in an options object; currently the only parameter honored is mapParams, which you can set to false to disable copying k/v pairs from the request body into req.params; instead, req.body will be overwritten with the parsed object. The default is to map params into req.params.
Throttle
restify ships with a fairly comprehensive implementation of Token bucket, with the ability to throttle on IP (or x-forwarded-for) and username (from req.username). You define "global" request rate and burst rate, and you can define overrides for specific keys. Note that you can always place this on per-URL routes to enable different request rates to different resources (if for example, one route, like /my/slow/database is much easier to overwhlem than /my/fast/memcache).
server.use(restify.throttle({ burst: 100, rate: 50, ip: true, overrides: { '192.168.1.1': { rate: 0, // unlimited burst: 0 } } }));If a client has consumed all of their available rate/burst, an HTTP response code of 429 Too Many Requests is returned.
Options:
Name Type Description rate Number Steady state number of requests/second to allow burst Number If available, the amount of requests to burst to ip Boolean Do throttling on a /32 (source IP) xff Boolean Do throttling on a /32 (X-Forwarded-For) username Boolean Do throttling on req.username overrides Object Per "key" overrides tokensTable Object Storage engine; must support put/get maxKeys Number If using the built-in storage table, the maximum distinct throttling keys to allow at a time Note that ip, xff and username are XOR'd.
Using an external storage mechanism for key/bucket mappings.
By default, the restify throttling plugin uses an in-memory LRU to store mappings between throttling keys (i.e., IP address) to the actual bucket that key is consuming. If this suits you, you can tune the maximum number of keys to store in memory with options.maxKeys; the default is 10000.
In some circumstances, you want to offload this into a shared system, such as Redis, if you have a fleet of API servers and you're not getting steady and/or uniform request distribution. To enable this, you can pass in options.tokensTable, which is simply any Object that supports put and get with a String key, and an Object value.
Conditional Request Handler
server.use(restify.conditionalRequest());You can use this handler to let clients do nice HTTP semantics with the "match" headers. Specifically, with this plugin in place, you would set res.etag=$yourhashhere, and then this plugin will do one of:
- return 304 (Not Modified) [and stop the handler chain]
- return 412 (Precondition Failed) [and stop the handler chain]
- Allow the request to go through the handler chain.
The specific headers this plugin looks at are:
- Last-Modified
- If-Match
- If-None-Match
- If-Modified-Since
- If-Unmodified-Since
Some example usage:
server.use(function setETag(req, res, next) { res.header('ETag', 'myETag'); res.header('Last-Modified', new Date()); }); server.use(restify.conditionalRequest()); server.get('/hello/:name', function(req, res, next) { res.send('hello ' + req.params.name); });Audit Logging
Audit logging is a special plugin, as you don't use it with .use(), but with the after event:
server.on('after', restify.auditLogger({ log: new Logger({ name: 'audit', stream: process.stdout }) }));You pass in the auditor a bunyan logger, and it will write out records at the info level. Records will look like this:
{ "name": "audit", "hostname": "your.host.name", "audit": true, "remoteAddress": "127.0.0.1", "remotePort": 57692, "req_id": "ed634c3e-1af0-40e4-ad1e-68c2fb67c8e1", "req": { "method": "GET", "url": "/foo", "headers": { "authorization": "Basic YWRtaW46am95cGFzczEyMw==", "user-agent": "curl/7.19.7 (universal-apple-darwin10.0) libcurl/7.19.7 OpenSSL/0.9.8r zlib/1.2.3", "host": "localhost:8080", "accept": "application/json" }, "httpVersion": "1.1", "trailers": {}, "version": "*" }, "res": { "statusCode": 200, "headers": { "access-control-allow-origin": "*", "access-control-allow-headers": "Accept, Accept-Version, Content-Length, Content-MD5, Content-Type, Date, X-Api-Version", "access-control-expose-headers": "X-Api-Version, X-Request-Id, X-Response-Time", "server": "Joyent SmartDataCenter 7.0.0", "x-request-id": "ed634c3e-1af0-40e4-ad1e-68c2fb67c8e1", "access-control-allow-methods": "GET", "x-api-version": "1.0.0", "connection": "close", "content-length": 158, "content-md5": "zkiRn2/k3saflPhxXI7aXA==", "content-type": "application/json", "date": "Tue, 07 Feb 2012 20:30:31 GMT", "x-response-time": 1639 }, "trailer": false }, "route": { "name": "GetFoo", "version": ["1.0.0"] }, "secure": false, "level": 30, "msg": GetFoo handled: 200", "time": "2012-02-07T20:30:31.896Z", "v": 0 }Request API
Wraps all of the node ServerRequest APIs, events and properties, plus the following.
header(key, [defaultValue])
Get the case-insensitive request header key, and optionally provide a default value (express-compliant):
req.header('Host'); req.header('HOST'); req.header('Accept', '*/*');accepts(type)
(express-compliant)
Check if the Accept header is present, and includes the given type.
When the Accept header is not present true is returned. Otherwise the given type is matched by an exact match, and then subtypes. You may pass the subtype such as html which is then converted internally to text/html using the mime lookup table.
// Accept: text/html req.accepts('html'); // => true // Accept: text/*; application/json req.accepts('html'); req.accepts('text/html'); req.accepts('text/plain'); req.accepts('application/json'); // => true req.accepts('image/png'); req.accepts('png'); // => falseis(type)
Check if the incoming request contains the Content-Type header field, and it contains the give mime type.
// With Content-Type: text/html; charset=utf-8 req.is('html'); req.is('text/html'); // => true // When Content-Type is application/json req.is('json'); req.is('application/json'); // => true req.is('html'); // => falseNote this is almost compliant with express, but restify does not have all the app.is() callback business express does.
log
Note that you can piggyback on the restify logging framework, by just using req.log. I.e.,:
function myHandler(req, res, next) { var log = req.log; log.debug({params: req.params}, 'Hello there %s', 'foo'); }The advantage to doing this is that each restify req instance has a new bunyan instance log on it where the request id is automatically injected in, so you can easily correlate your high-throughput logs together.
getLogger(component)
Shorthand to grab a new bunyan instance that is a child component of the one restify has:
var log = req.getLogger('MyFoo');Properties
Name Type Description contentLength Number short hand for the header content-length contentType String short hand for the header content-type href String url.parse(req.url) href log Object bunyan logger you can piggyback on id String A unique request id (x-request-id) path String cleaned up URL path query String the query string only secure Boolean Whether this was an SSL request time Number the time when this request arrived (ms since epoch) Response API
Wraps all of the node ServerResponse APIs, events and properties, plus the following.
header(key, value)
Get or set the response header key.
res.header('Content-Length'); // => undefined res.header('Content-Length', 123); // => 123 res.header('Content-Length'); // => 123 res.header('foo', new Date()); // => Fri, 03 Feb 2012 20:09:58 GMTcache([type], [options])
Sets the cache-control header. type defaults to _public_, and options currently only takes maxAge.
res.cache();status(code)
Sets the response statusCode.
res.status(201);send([status], body)
You can use send() to wrap up all the usual writeHead(), write(), end() calls on the HTTP API of node. You can pass send either a code and body, or just a body. body can be an Object, a Buffer, or an Error. When you call send(), restify figures out how to format the response (see content-negotiation, above), and does that.
res.send({hello: 'world'}); res.send(201, {hello: 'world'}); res.send(new BadRequestError('meh'));json([status], body)
Short-hand for:
res.contentType = 'json'; res.send({hello: 'world'});Properties
Name Type Description code Number HTTP status code contentLength Number short hand for the header content-length charSet String In conjunction with contentType, you can explicitly set the charSet to be written in the content-type header contentType String short hand for the header content-type headers Object response headers id String A unique request id (x-request-id) Setting the default headers
You can change what headers restify sends by default by setting the top-level property defaultResponseHeaders. This should be a function that takes one argument data, which is the already serialized response body. data can be either a String or Buffer (or null). The this object will be the response itself.
var restify = require('restify'); restify.defaultResponseHeaders = function(data) { this.header('Server', 'helloworld'); }; restify.defaultResponseHeaders = false; // disable altogetherDTrace
One of the coolest features of restify is that it automatically creates DTrace probes for you whenever you add a new route/handler. The easiest way to explain this is with an example:
var restify = require('restify'); var server = restify.createServer({ name: 'helloworld' }); server.use(restify.acceptParser(server.acceptable)); server.use(restify.authorizationParser()); server.use(restify.dateParser()); server.use(restify.queryParser()); server.use(restify.urlEncodedBodyParser()); server.use(function slowHandler(req, res, next) { setTimeout(function() { return next(); }, 250); }); server.get({path: '/hello/:name', name: 'GetFoo'}, function respond(req, res, next) { res.send({ hello: req.params.name }); return next(); }); server.listen(8080, function() { console.log('listening: %s', server.url); });So we've got our typical "hello world" server now, with a slight twist; we introduced an artificial 250ms lag. Also, note that we named our server, our routes, and all of our handlers (functions); while that's optional, it does make DTrace much more usable. So, if you started that server, then looked for DTrace probes, you'd see something like this:
dtrace -l -P helloworld* ID PROVIDER MODULE FUNCTION NAME 23491 helloworld10254 module func getfoo-start 23492 helloworld10254 module func getfoo-done 23493 helloworld10254 module func getfoo-parseAccept-start 23494 helloworld10254 module func getfoo-parseAccept-done 23495 helloworld10254 module func getfoo-parseAuthorization-start 23496 helloworld10254 module func getfoo-parseAuthorization-done 23497 helloworld10254 module func getfoo-parseDate-start 23498 helloworld10254 module func getfoo-parseDate-done 23499 helloworld10254 module func getfoo-parseQueryString-start 23500 helloworld10254 module func getfoo-parseQueryString-done 23501 helloworld10254 module func getfoo-parseUrlEncodedBody-start 23502 helloworld10254 module func getfoo-parseUrlEncodedBody-done 23503 helloworld10254 module func getfoo-slowHandler-start 23504 helloworld10254 module func getfoo-slowHandler-done 23505 helloworld10254 module func getfoo-respond-start 23506 helloworld10254 module func getfoo-respond-doneWhat restify does is autogenerate a probe for each route/handler on start and finish, and uses the names found to do so. If there's no names, it fills in based on the method, url and index in the chain. The probe signatures generated look like:
Start Probes
$route-start and $route-$handler-start
Field Type Description id int cookie you can use to correlate request/response url char * request url user-agent char * value of user-agent header user char * req.username (i.e., the authenticated party) content-type char * value of content-type header content-length int value of content-length header Done Probes
$route-done and $route-$handler-done
Field Type Description id int cookie you can use to correlate request/response statusCode int HTTP response code content-type char * value of content-type header content-length int value of content-length header Now, if you wanted to say get a breakdown of latency by handler, you could do something like this:
#!/usr/sbin/dtrace -s #pragma D option quiet helloworld*:::getfoo-*-start { tracker[arg0, substr(probename, 0, rindex(probename, "-"))] = timestamp; } helloworld*:::getfoo-*-done /tracker[arg0, substr(probename, 0, rindex(probename, "-"))]/ { this->name = substr(probename, 0, rindex(probename, "-")); @[this->name] = quantize(((timestamp - tracker[arg0, this->name]) / 1000000)); tracker[arg0, substr(probename, 0, rindex(probename, "-"))] = 0; }So running the server in one terminal:
node helloworld.jsThe D script in another:
./helloworld.dHit the server a few times with curl:
curl http://localhost:8080/hello/mcavageThen Ctrl-C the D script, and you'll see the "slowHandler" at the bottom of the stack, bucketized that it's the vast majority of latency in this pipeline
./guide.d ^C getfoo-parseAuthorization value ------------- Distribution ------------- count -1 | 0 0 |@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ 4 1 | 0 getfoo-parseDate value ------------- Distribution ------------- count -1 | 0 0 |@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ 4 1 | 0 getfoo-parseQueryString value ------------- Distribution ------------- count -1 | 0 0 |@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ 4 1 | 0 getfoo-parseUrlEncodedBody value ------------- Distribution ------------- count -1 | 0 0 |@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ 4 1 | 0 getfoo-parseAccept value ------------- Distribution ------------- count -1 | 0 0 |@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ 3 1 |@@@@@@@@@@ 1 2 | 0 getfoo-respond value ------------- Distribution ------------- count 0 | 0 1 |@@@@@@@@@@ 1 2 |@@@@@@@@@@@@@@@@@@@@ 2 4 | 0 8 | 0 16 | 0 32 |@@@@@@@@@@ 1 64 | 0 getfoo-slowHandler value ------------- Distribution ------------- count 64 | 0 128 |@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ 4 256 | 0Client APIThere are actually three separate clients shipped in restify:
- JsonClient: sends and expects application/json
- StringClient: sends url-encoded request and expects text/plain
- HttpClient: thin wrapper over node's http/https libraries
The idea being that if you want to support "typical" control-plane REST APIs, you probably want the JsonClient, or if you're using some other serialization (like XML) you'd write your own client that extends the StringClient. If you need streaming support, you'll need to do some work on top of the HttpClient, as StringClient and friends buffer requests/responses.
All clients support retry with exponential backoff for getting a TCP connection; they do not perform retries on 5xx error codes like previous versions of the restify client. You can set retry to false to disable this logic altogether. Also, all clients support a connectTimeout field, which is use on each retry. The default is not to set a connectTimeout, so you end up with the node.js socket defaults.
Here's an example of hitting the Joyent CloudAPI:
var restify = require('restify'); // Creates a JSON client var client = restify.createJsonClient({ url: 'https://us-west-1.api.joyentcloud.com' }); client.basicAuth('$login', '$password'); client.get('/my/machines', function(err, req, res, obj) { assert.ifError(err); console.log(JSON.stringify(obj, null, 2)); });Note that all further documentation refers to the "short-hand" form of methods like get/put/del which take a string path. You can also pass in an object to any of those methods with extra params (notably headers):
var options = { path: '/foo/bar', headers: { 'x-foo': 'bar' }, retry: { 'retries': 0 }, agent: false }; client.get(options, function(err, req, res) { .. });JsonClient
The JSON Client is the highest-level client bundled with restify; it exports a set of methods that map directly to HTTP verbs. All callbacks look like function(err, req, res, [obj]), where obj is optional, depending on if content was returned. HTTP status codes are not interpreted, so if the server returned 4xx or something with a JSON payload, obj will be the JSON payload. err however will be set if the server returned a status code >= 400 (it will be one of the restify HTTP errors). If obj looks like a RestError:
{ "code": "FooError", "message": "some foo happened" }then err gets "upconverted" into a RestError for you. Otherwise it will be an HttpError.
createJsonClient(options)
var client = restify.createJsonClient({ url: 'https://api.us-west-1.joyentcloud.com', version: '*' });Options:
Name Type Description accept String Accept header to send connectTimeout Number Amount of time to wait for a socket dtrace Object node-dtrace-provider handle headers Object HTTP headers to set in all requests log Object bunyan instance retry Object options to provide to node-retry; defaults to 3 retries url String Fully-qualified URL to connect to userAgent String user-agent string to use; restify inserts one, but you can override it version String semver string to set the accept-version get(path, callback)
Performs an HTTP get; if no payload was returned, obj defaults to {} for you (so you don't get a bunch of null pointer errors).
client.get('/foo/bar', function(err, req, res, obj) { assert.ifError(err); console.log('%j', obj); });head(path, callback)
Just like get, but without obj:
client.head('/foo/bar', function(err, req, res) { assert.ifError(err); console.log('%d -> %j', res.statusCode, res.headers); });post(path, object, callback)
Takes a complete object to serialize and send to the server.
client.post('/foo', { hello: 'world' }, function(err, req, res, obj) { assert.ifError(err); console.log('%d -> %j', res.statusCode, res.headers); console.log('%j', obj); });put(path, object, callback)
Just like post:
client.put('/foo', { hello: 'world' }, function(err, req, res, obj) { assert.ifError(err); console.log('%d -> %j', res.statusCode, res.headers); console.log('%j', obj); });del(path, callback)
del doesn't take content, since you know, it should't:
client.del('/foo/bar', function(err, req, res) { assert.ifError(err); console.log('%d -> %j', res.statusCode, res.headers); });StringClient
StringClient is what JsonClient is built on, and provides a base for you to write other buffering/parsing clients (like say an XML client). If you need to talk to some "raw" HTTP server, then StringClient is what you want, as it by default will provide you with content uploads in application/x-www-form-url-encoded and downloads as text/plain. To extend a StringClient, take a look at the source for JsonClient. Effectively, you extend it, and set the appropriate options in the constructor and implement a write (for put/post) and parse method (for all HTTP bodies), and that's it.
createStringClient(options)
var client = restify.createStringClient({ url: 'https://example.com' })get(path, callback)
Performs an HTTP get; if no payload was returned, data defaults to '' for you (so you don't get a bunch of null pointer errors).
client.get('/foo/bar', function(err, req, res, data) { assert.ifError(err); console.log('%s', data); });head(path, callback)
Just like get, but without data:
client.head('/foo/bar', function(err, req, res) { assert.ifError(err); console.log('%d -> %j', res.statusCode, res.headers); });post(path, object, callback)
Takes a complete object to serialize and send to the server.
client.post('/foo', { hello: 'world' }, function(err, req, res, data) { assert.ifError(err); console.log('%d -> %j', res.statusCode, res.headers); console.log('%s', data); });put(path, object, callback)
Just like post:
client.put('/foo', { hello: 'world' }, function(err, req, res, data) { assert.ifError(err); console.log('%d -> %j', res.statusCode, res.headers); console.log('%s', data); });del(path, callback)
del doesn't take content, since you know, it should't:
client.del('/foo/bar', function(err, req, res) { assert.ifError(err); console.log('%d -> %j', res.statusCode, res.headers); });HttpClient
HttpClient is the lowest-level client shipped in restify, and is basically just some sugar over the top of node's http/https modules (with HTTP methods like the other clients). It is useful if you want to stream with restify. Note that the event below is unfortunately named result and not response (because Event 'response' is already used).
client = restify.createClient({ url: 'http://127.0.0.1' }); client.get('/str/mcavage', function(err, req) { assert.ifError(err); // connection error req.on('result', function(err, res) { assert.ifError(err); // HTTP status code >= 400 res.body = ''; res.setEncoding('utf8'); res.on('data', function(chunk) { res.body += chunk; }); res.on('end', function() { console.log(body); }); }); });Or a write:
client.post(opts, function(err, req) { assert.ifError(connectErr); req.on('result', function(err, res) { assert.ifError(err); res.body = ''; res.setEncoding('utf8'); res.on('data', function(chunk) { res.body += chunk; }); res.on('end', function() { console.log(res.body); }); }); req.write('hello world'); req.end(); });Note that get/head/del all call req.end() for you, so you can't write data over those. Otherwise, all the same methods exist as JsonClient/StringClient.
One wishing to extend the HttpClient should look at the internals and note that read and write probably need to be overridden.
basicAuth(username, password)
Since it hasn't been mentioned yet, this convenience method (available on all clients), just sets the Authorization header for all HTTP requests:
client.basicAuth('mark', 'mysupersecretpassword');
NoSQL has been one of the most talked about topics over the past couple of months. This tutorial will introduce you to CouchDB, a NoSQL implementation and teach you how to get started with the platform.
What is NoSQL?
NoSQL is schema free — you don’t need to decide the structure up front.
NoSQL [not only SQL] is a movement towards document stores that do not make use of the relational model. The fundamental paradigm shift is in the way they store data. For example, when you’d need to store data about an invoice, in RDBMS you’d need to distill this information into tables and then use a server-side language to transform this data back into real life objects. On the other hand, in NoSQL, you just store the invoice. NoSQL is schema free, which means you don’t need to design your tables and structure up front — you can simply start storing new values.
Continuing the invoice example, some invoices may include a VAT number, some may not. In a RDBMS, you’d need to tell your table to first accept a VAT number and then that it could possibly be null. In NoSQL, however, you can just store invoices with or without a VAT number — there is no schema. Keep in mind that NoSQL is not a silver bullet. If your data is truly relational, sticking with your RDBMS would be the right choice.
Querying NoSQL Databases
MapReducing has benefits over SQL queries because the map/reduce task can be distributed among multiple nodes, something not possible in RDBMS.
NoSQL databases use map/reduce to query and index the database. In RDBMS, you run a query joining multiple tables together to first create a pool of data and then the query runs creating a resultset, a subset of the overall data. In NoSQL, you use map/reduce to create a ‘view’ (similar to a resultset) this view is a subset of the overall data.
Map is essentially extracting data and reduce, data aggregation. The more familiar you are with RDBMS, the more difficult grasping map/reduce will be. MapReducing benefits over SQL queries because the map/reduce task can be distributed among multiple nodes, something not possible in RDBMS. Adding a new record to the database does not always constitute the map/reduce task being completely rerun.
Introducing CouchDB
A few facts about CouchDB that you should know:
- CouchDB is a JSON document-oriented database written in Erlang.
- It is a highly concurrent database designed to be easily replicable, horizontally, across numerous devices and be fault tolerant.
- It is part of the NoSQL generation of databases.
- It is an open source Apache foundation project.
- It allows applications to store JSON documents via its RESTful interface.
- It makes use of map/reduce to index and query the database.
Major Benefits of CouchDB
- JSON Documents – Everything stored in CouchDB boils down to a JSON document.
- RESTful Interface – From creation to replication to data insertion, every management and data task in CouchDB can be done via HTTP.
- N-Master Replication – You can make use of an unlimited amount of ‘masters’, making for some very interesting replication topologies.
- Built for Offline – CouchDB can replicate to devices (like Android phones) that can go offline and handle data sync for you when the device is back online.
- Replication Filters – You can filter precisely the data you wish to replicate to different nodes.
Putting It All Together
CouchDB is a database designed to run on the internet of today.
CouchDB allows you to write a client side application that talks directly to the Couch without the need for a server side middle layer, significantly reducing development time. With CouchDB, you can easily handle demand by adding more replication nodes with ease. CouchDB allows you to replicate the database to your client and with filters you could even replicate that specific user’s data.
Having the database stored locally means your client side application can run with almost no latency. CouchDB will handle the replication to the cloud for you. Your users could access their invoices on their mobile phone and make changes with no noticeable latency, all whilst being offline. When a connection is present and usable, CouchDB will automatically replicate those changes to your cloud CouchDB.
CouchDB is a database designed to run on the internet of today for today’s desktop-like applications and the connected devices through which we access the internet.
Step 1 – Installing CouchDB
The easiest way to get CouchDB up and running on your system is to head to CouchOne and download a CouchDB distribution for your OS — OSX in my case. Download the zip, extract it and drop CouchDBX in my applications folder (instructions for other OS’s on CouchOne).
Finally, open CouchDBX.
Step 2 – Welcome to Futon
After CouchDB has started, you should see the Futon control panel in the CouchDBX application. In case you can’t, you can access Futon via your browser. Looking at the log, CouchDBX tells us CouchDB was started at http://127.0.0.1:5984/ (may be different on your system). Open a browser and go to http://127.0.0.1:5984/_utils/ and you should see Futon.
Throughout the rest of this tutorial I will be using Futon in Firefox. I’ll also have Firebug and the console view open to see all the HTTP requests Futon is sending behind the scenes. This is useful as your application can do everything Futon is doing. Let’s go ahead and create a database called mycouchshop.
CouchDB jQuery Plugin
Futon is actually using a jQuery plugin to interact with CouchDB. You can view that plugin at http://127.0.0.1:5984/_utils/script/jquery.couch.js (bear in mind your port may be different). This gives you a great example of interacting with CouchDB.
Step 3 – Users in CouchDB
CouchDB, by default, is completely open, giving every user admin rights to the instance and all its databases. This is great for development but obviously bad for production. Let’s go ahead and setup an admin. In the bottom right, you will see “Welcome to Admin Party! Everyone is admin! Fix this”.
Go ahead and click fix this and give yourself a username and password. This creates an admin account and gives anonymous users access to read and write operations on all the databases, but no configuration privileges.
More on Users
In CouchDB it would be unwise to create a single super user and have that user do all the read/write.
Users in CouchDB can be a little confusing to grasp initially, specially if you’re used to creating a single user for your entire application and then managing users yourself within a users table (not the MySQL users table). In CouchDB, it would be unwise to create a single super user and have that user do all the read/write, because if your app is client-side then this super user’s credentials will be in plain sight in your JavaScript source code.
CouchDB has user creation and authentication baked in. You can create users with the jQuery plugin using $.couch.signup(). These essentially become the users of your system. Users are just JSON documents like everything else so you can store any additional attributes you wish like email for example. You can then use groups within CouchDB to control what documents each user has write access to. For example, you can create a database for that user to which they can write to and then add them to a group with read access to the other databases as required.
Step 4 – Creating a Product Document
Now let’s create our first document using Futon through the following steps:
- Open the mycouchshop database.
- Click “New Document”.
- Click “Add Field” to begin adding data to the JSON document. Notice how an ID is pre-filled out for you, I would highly advise not changing this. Add key “name” with the value of “Nettuts CouchDB Tutorial One”.
- Make sure you click the tick next to each attribute to save it.
- Click “Save Document”.
Go up a level, back to the database and you should see one document listed with the previous ID as the key and a value beginning with{rev: . This is the JSON document you just created.
Step 5 – Updating a Document
CouchDB is an append only database — new updates are appended to the database and do not overwrite the old version. Each new update to a JSON document with a pre-existing ID will add a new revision. This is what the automatically inserted revision key signifies. Follow the steps below to see this in action:
- Viewing the contents of the mycouchshop database, click the only record visible.
- Add another attribute with the key “type” and the value “product”.
- Hit “Save Document”.
After hitting save, a new revision key should be visible starting with the number 2. Going back a level to the mycouchshop database view, you will still see just one document, this is the latest revision of our product document.
Revisions
While CouchDB uses revisions internally, try to not lean on it too much. The revisions can be cleaned through Futon quite easily and it is not designed to be used as a revision control system. CouchDB uses the revisions as part of its replication functionality.
Step 6 – Creating a Document Using cURL
I’ve already mentioned that CouchDB uses a RESTful interface and the eagle eyed reader would have noticed Futon using this via the console in Firebug. In case you didn’t, let’s prove this by inserting a document using cURL via the Terminal.
First, let’s create a JSON document with the below contents and save it to the desktop calling the file person.json.
{ "forename": "Gavin", "surname": "Cooper", "type": "person" }Next, open the terminal and execute cd ~/Desktop/ putting you in the correct directory and then perform the insert with curl -X POST http://127.0.0.1:5984/mycouchshop/ -d @person.json -H "Content-Type: application/json". CouchDB should have returned a JSON document similar to the one below.
{"ok":true,"id":"c6e2f3d7f8d0c91ce7938e9c0800131c","rev":"1-abadd48a09c270047658dbc38dc8a892"}This is the ID and revision number of the inserted document. CouchDB follows the RESTful convention and thus:
- POST – creates a new record
- GET – reads records
- PUT – updates a record
- DELETE – deletes a record
Step 7 – Viewing All Documents
We can further verify our insert by viewing all the documents in our mycouchshop database by executing curl -X GET http://127.0.0.1:5984/mycouchshop/_all_docs.
Step 8 – Creating a Simple Map Function
Viewing all documents is fairly useless in practical terms. What would be more ideal is to view all product documents. Follow the steps below to achieve this:
- Within Futon, click on the view drop down and select “Temporary View”.
- This is the map reduce editor within Futon. Copy the code below into the map function.
function (doc) { if (doc.type === "product" && doc.name) { emit(doc.name, doc); } }- Click run and you should see the single product we added previously.
- Go ahead and make this view permanent by saving it.
After creating this simple map function, we can now request this view and see its contents over HTTP using the following command curl -X GET http://127.0.0.1:5984/mycouchshop/_design/products/_view/products.
A small thing to notice is how we get the document’s ID and revision by default.
Step 9 – Performing a Reduce
To perform a useful reduce, let’s add another product to our database and add a price attribute with the value of 1.75 to our first product.
{ "name": "My Product", "price": 2.99, "type": "product" }For our new view, we will include a reduce as well as a map. First, we need to map defined as below.
function (doc) { if (doc.type === "product" && doc.price) { emit(doc.id, doc.price); } }The above map function simply checks to see if the inputted document is a product and that it has a price. If these conditions have been met, the products price is emitted. The reduce function is below.
function (keys, prices) { return sum(prices); }The above function takes the prices and returns the sum using one of CouchDB’s built in reduce functions. Make sure you check the reduce option in the top right of the results table as you may otherwise be unable to see the results of the reduce. You may need to do a hard-refresh on the page to view the reduce option
Conclusion
In this tutorial, we took a brief but focused look at CouchDB. We saw the potential power of CouchDB and how easy it is to get started. I’m sure you have plenty of questions at this point so feel free to chime in below. Thank you so much for reading!
SequelizeThe Sequelize library provides easy access to MySQL, SQLite or PostgreSQL databases by mapping database entries to objects and vice versa. To put it in a nutshell... it's an ORM (Object-Relational-Mapper). The library is written entirely in JavaScript and can be used in the Node.JS environment.
Blogposts/Changes
- v1.4.1: deprecation of node < 0.6, logging customization, ...
- v1.4.0: postgresql, connection pooling, ...
- v1.3.0: migrations, cross-database, validations, new listener notation, ...
- v1.2.1: changes some defaults and some interfaces
- v1.0.0: complete rewrite
Features
- Schema definition
- Schema synchronization/dropping
- Easy definition of class/instance methods
- Instance saving/updating/dropping
- Asynchronous library
- Associations
- Importing definitions from single files
Documentation, Examples and Updates
You can find the documentation and announcements of updates on the project's website. If you want to know about latest development and releases, follow me on Twitter. Also make sure to take a look at the examples in the repository. The website will contain them soon, as well.
Collaboration
I'm glad to get pull request if any functionality is missing or something is buggy. But please ... run the tests before you send me the pull request.
Now if you want to contribute but don't really know where to begin don't worry, the steps below will guide you to have a sequelize contributor's environment running in a couple minutes.
1. Prepare the environment
All the following steps consider you already have npm installed in your node.js version 0.4.6 or higher
1.1 MySQL and other external dependencies
Contributing to sequelize requires you to have MySQL up and running in your local environment. The reason for that is that we have test cases that runs against an actual MySQL server and make sure everything is always working.
That is also one of the reasons your features must come with tests: let's make sure sequelize will stay awesome as more features are added as well as that fixed bugs will never come back.
Well, after installing MySQL you also need to create the sequelize test database:
$ echo "CREATE DATABASE sequelize_test;" | mysql -urootCLEVER NOTE: your local MySQL install must be with username root without password. If you want to customize that just hack in the tests, but make sure to don't commit your credentials, we don't want to expose your personal data in sequelize codebase ;)
AND ONE LAST THING: Sequelize also supports SQLite. So this should be working on your machine as well :)
2. Install the dependencies
Just "cd" into sequelize directory and run npm install, see an example below:
$ cd path/to/sequelize $ npm install3. Run the tests
In order to run the tests you got to run jasmine-node against the spec directory. By the way, there is where you will write new tests if that's the case.
All you need is to run ./node_modules/.bin/jasmine-node spec/, although this is kinda long and boring, so we configures a NPM task and made that less laborious to you :)
4. That's all
Just commit and send pull requests.
Happy hacking and thank you for contributing
Build statusThe automated tests we talk about just so much are running on Travis public CI, here is its status:
Adding to this page
When you add a framework to this page, have a look at how others have done so, it should be a single item, with a link to the project's source code, and a short description (one line after formatting has been applied).
If you see a module without a description, feel free to edit the page and add it in, any contributions are appreciated.
When you edit this list, also add your module to library-compatibility so that users will know which versions of Node it was tested with.
Table of contents
- barista — A fast, flexible URL router & generator, similar to Rails / merb (repo)
- beanpoll — Universal router with syntactic sugar
- beeline — A laughably simplistic router for node.js
- biggie-router — Router for the biggie framework. Inspired by Sinatra and JQuery.
- Birbal — Pretty darn simple and clever router. Work underway to turn into a microframework!
- choreographer — Your server is my stage — dirt simple URL routing for Node.js. Easy to use, easy to understand. Sinatra-style API.
- clutch — no-frills web request router, supporting nested routes, regexp parameters
- connect-router — connect/express router
- connect — Robust high performance middleware framework
- copperhead — Connect compatible router middleware that supports content negotiation
- crossroads — Powerful and Flexible routing library, works on the client-side as well and have support for advanced features.
- dispatch — Regular expression URL dispatcher for Connect
- escort — High-performance, advanced routing and URL generation
- filter-chain — Java ServletFilter style request chaining
- journey — liberal JSON-only HTTP request router
- node-router — Simple Sinatra-like http server based on fu.js from the original node-chat demo.
- node.routes.js — A simple url router
- nozzle — web app/service framework
- Route66 — Routing middleware for Connect 2.0. Request params, simple syntax.
- router — A simple router with regex and sinatra like parameter support
- sherpa — HTTP router/generator with support for regex and parameters
- antinode — A simple static web server
- bastard — Automatic minification, in-memory cache, automatic fingerprinting.
- connect — Connect's static() middleware provides flexible, high-performance, feature-rich static file serving
- Lactate — An exceedingly fast & simple static assets handler, with a few electives.
- Lightnode — Easy to understand. Fast. Provides simple framework. (hierarchical servers, delegation, caching, virtual hosts).
- navajo - A simple web server with the ability to run PHP or Node code as well as serve static files (alpha)
- Nitrode — A lightweight, super fast HTTP 1.1 with internal JS API
- node-paperboy — A node.js module for delivering static files
- node-static — A simple, rfc 2616-compliant static file server module with built-in caching
- nodetoy — Static JSON file server that supports GET, POST and DELETE
- nserve — A local development server, allowing for adjustable file transfer rate, mock web services and live reload.
- oui — Web service server with great static files support
- send — Connect's robust static file server extracted for general use, supports range requests, conditional-GETs etc
- static-resource - Static resource handling for node.js
- static - Static file handler using asynchronous streaming as JSGI middleware
- statify — A static file server to make any directory available via HTTP from the command line.
- Trailer — Everyone's favorite backend.
- wup — A simple web server for quick tests. Install via npm install -g wup, start with wup, go to http://localhost:8080/.
- (fab) — A modular and concise async web framework for node.js
- frank — yet another sinatra-like microframework for node
- jqNode — Simple jQuery-esque API for small projects.
- kaph — Not framework
- Layers — A module to help create a layered web app with Express.
- Monorail.js — Ultra leightweight MVC web framework.
- mvc — A lightweight mvc layer for express inspired by ZendFramework on php.
- Nerve — Microframework with simple array-based syntax for defining an app on top of node. (node.JS 0.1.30)
- Ni — A minimalistic Node module / framework that makes setting up an MVC project a breeze
- Picard
- Pipe-Layer — Asynchronous HTTP router.
- rapid-rest — Minimal overhead full-fledged REST server. Benchmark
- restmvc.js — A simple library to help you quickly create RESTful webservices using Express, Mongoose, and MongoDB.
- seki — simple front-end to an independent SPARQL
- simplex (October 2009, node.JS 0.1.14)
- vroom — A simple resource oriented web framework built on top of Node.js (November 2009, node.JS 0.1.16)
- zappa — CoffeeScript minimalist interface to express, socket.io and others.
- actionHero — actionHero is a minimalist transactional API framework for sockets and http clients
- archetype — A web framework leveraging Node.js
- aries — Annotation based MVC framework
- BaseJump — A RAD NodeJS Framework - Built on top of Connect (alpha release)
- blueprint — Blueprint for a Startup. Middleware, & MVC routing over Node.js & Mongoose
- broke — A porting of the most famous Django Web Framework
- Capsela — A high-level, promises-based web framework with an emphasis on testability (see website)
- Cargobox — Express port with better OOP
- chain — An evented convention for building Node Applications (Stopped Development, for ejsgi)
- Coffeemate — Push coffee-script into web development!
- COKE — A lightweight MVC framework base on Express that speeds up your web development. (Rails like)
- Crux — An MVC web application framework and project management utility, similar in some ways to Rails.
- Derby — MVC framework making it easy to write realtime, collaborative applications that run in both Node.js and browsers
- djangode — A framework that borrows some useful concepts from Django (March 2010, node.JS 0.1.30)
- drty — A Django port to NodeJS. Many Django features, still under heavy development. (January, 2011, 0.2.6)
- Drumkit --- DrumKit is a plugin-powered, full-stack Web development framework for Node.js.
- Express — A robust feature rich web development framework inspired by Sinatra
- Ext Core for NodeJS — Templating and some basic tricks of ExtCore and ExtJS(XTemplate,Ext.util.format)
- Flatiron — an adaptable framework for building modern web applications. URL Routing with Director, templating with Plates, data management with Resourceful, middleware with Union (connect-compatible), plugins with Broadway, logging with Winston.
- Geddy — A hackable Web-app development framework similar to Merb/Rails/Pylons/Django
- GenJi — A low-level loosely coupled web framework for nodejs
- Grasshopper — A feature-rich and flexible MVC framework for web applications and services, with integrated dependency injection.
- JaxServer — Application server and framework with template and css engines
- jimi — A framework for writing modular web applications in node.js (tutorial)
- josi — An MVC web framework that's easy to pick up but doesn't get in the way. Also see here
- Kassit — rapid building of client-side AJAX applications using Backbone and CoffeeScript
- Katana — Easy to use, hmvc scalable web framework for any Node.js samurai
- Kiss.js — Web framework for node.js in CoffeeScript. Object-oriented, simple and sexy.
- Locomotive — Effective MVC web development for Node.js inspired by Ruby on Rails
- merlin — Object-oriented MVC framework relying on a robust set of conventions to minimize code. Also ships with optional plugins providing basic CMS-like functionality.
- Meryl — Minimalist web framework! See wiki
- Monorail.js — Ultra leightweight MVC web framework.
- N-Ext — Ext.core, Ext.util and Ext.data packages in your NodeJS apps (includes a MongoDB proxy based on node-mongodb-native)
- node-extjs — Run ExtJS4 data models on Node.js
- nodemachine — A port of WebMachine to Node.js
- nodepress — High-level web framework for nodejs, can be used as a blog by default
- PieJS — A rapid development MVC framework. Inspired and similar in style/convention to CakePHP. In very active development.
- pintura — REST-based web framework/middleware stack built for Ajax-style JSON-driven applications
- Protos — Web Application Framework
- QuickWeb — An application server for Node.js
- RailwayJS — An MVC web framework, similar to Ruby on Rails, Express/Connect-compatible. Also see here (en) and here (jp)
- Sayndo — Fast and flexible web server with customized routing and authorization
- Seek — Small Javascript Web framework - mostly for learning purpose
- SocketStream — A fast full-stack real-time web framework for single-page apps
- spludo — A full featured web framework. Fully Async + MVC with DI, AOP and Storages. (tutorial + user-guide)
- Stick — A modular JSGI middleware composition layer and application framework
- Tower — Full Stack Web Framework for Node.js and the Browser, like Rails.
- webjs — Simple HTTP / TCP development framework
- Zeppelin — An early stage, low friction cloud development framework
- auth - Handles Authentication (HTTP and cookie based).
- cascade — Sequentially attempts multiple middleware apps.
- commonlogger - A logger of HTTP requests.
- compact - Join and compress frontend javascript.
- compress - Gzip compresses (using node-compress) the response when appropriate based on request headers.
- conditional - Handles conditional HTTP requests (If-Modified-Since, etc.)
- contentlength - Sets Content-Length header.
- csrf - Checks HTTP request for possible cross-site request forgery, flags dangerous requests.
- error - Catches uncaught errors and converts to appropriate HTTP error responses.
- extension - Transforms .extension to a RESTful Accept header
- head - Handles HEAD requests (stripping body).
- http-params - Converts HTTP parameters http- to headers.
- media - Performs content type negotiation (per RFC2616) delegating to appropriate media handler.
- redirect - Redirects to other URLs
- rewriter - Rewrites defined paths to other paths.
- routes - Simple RegExp based router
- session - Session manager with pluggable storage handling
- static - Static file handler using asynchronous streaming.
- transporter - Share modules with browser, works RequireJS and Yabble
- urlmap - Maps to different apps by path/URL
- xsite - Handles JSONP, window.name, and cross-origin XHR (CORS).
- bundle-up — A simple asset manager middleware for managing css and js files.
- connect_facebook - Facebook session support for Connect
- connect_json - Support for parsing JSON requests and sending JSON responses in Connect
- connect-airbrake — Airbrake error reporting auto-setup middleware
- connect-assetmanager - Asset manager for Connect for handling CSS/JS files
- connect-assets - Compiled CSS/JS asset pipeline inspired by Rails 3.1
- connect-auth — Connect authentication middleware, provides out-of-the-box implementations of HTTP (Basic & Digest), Twitter, Facebook, BitBucket, Janrain, Yahoo, Sina, Google, OAuth (1.0 server), Github and a couple of others....
- connect-compiler — Development middleware to dynamically recompile derived files at serve-time.
- connect-dojo — Connect middleware exposing the Dojo Toolkit
- connect-force-domain — force all visitors onto a single domain
- connect-http-signature — middleware wrapper for Joyent's HTTP Signature reference implementation
- connect-proxy — Retrieve originating ip/host values when proxying to your connect app
- connect-roles — Dynamic roles based authorization for connect/express, designed to work well with passport and everyauth.
- connect-rpx - Use RPX with Node.js and Connect
- connect-views - Serve your jades, markdowns, styluses like a static files
- cookie-sessions — Secure cookie-based session store
- dispatch — Regular expression URL dispatcher
- everyauth — Connect authentication and authorization middleware, modular, configurable, supporting password, OpenId, Google, OAuth, Twitter, LinkedIn, Yahoo, Readability, Dropbox, Justin.tv, Vimeo, Tumblr, OAuth2, Facebook, GitHub, Instagram, Foursquare, Box.net, LDAP
- express-chromeframe — Dead simple middleware to enable chromeframe on connect/express applications.
- express-errors — Simple error handling
- facebook-wrapper — Basic wrapper to the Facebook API, designed to work with Connect and Express
- form2json — Decoder for form-urlencoded data that supports arrays of nested objects
- formaline - full-featured module for handling form POSTs/PUTs (multipart/form-data, application/x-www-form-urlencoded ) and fast parsing of file uploads, it speaks JSON and it is also ready for use with middlewares like connect.
- http-accept — Connect compatible middleware that parses HTTP Accept header fields
- merge-js — Simple connect middleware for merging multiple js files and uglifying the result.
- mincer — direct sprockets assets manager port, with middleware for connect
- minj — Serve minified javascript files with Connect/Express
- monomi — Provides tools for handling tablet, touch, and mobile browsers
- node_signedcookies — Extends Express's cookieParser() to read/write signed cookies.
- node-facebook-session-cookie — eats facebook cookies from client FB.login() and makes the session available as req.fb_session
- passport — Simple, modular, and unobtrusive authentication framework for Connect and Express.
- pound — Pound is an high-level interface for Piler - The Awesome Asset Manager for Node.js
- quip — Chainable HTTP response API
- resource-router — A resource-oriented router to replace the default routing in express
- resty — Quickly and simply build REST APIs with this connect middleware
- session-web-sockets — Pass session to (Socket.IO-node) in a secure manner. Originally forked from bmeck/session-web-sockets
- session.js — super simple session middleware for node.js, even has optional "magic" sessions which monkey patch the httpServer with one line!
- trust-reverse-proxy - Trust (SSL) connections coming from (a) specific reverse prox(y)(ies)
- eventpipe — Provides an Event Pipe with the same API as node.js' EventEmitter
- exedra — Express routes & functions loader
- express-twitter — Twitter-specific OAuth support
- googleclientlogin — Log in to Google services using CllientLogin method
- http-auth — Node.js package for HTTP basic and digest access authentication.
- http-auth2 — HTTP basic authentication that supports multiple logins.
- http-proxy-selective — Proxy server replace some remote static files with local ones
- node-evented — Extended EventEmitter
- node-file-cache - Very small file cache for node.js http server.
- node-force-domain — Force multiple domains to redirect (301) to a default one in your Express project.
- Node-Http-Rewrite-Proxy — This module proxies and rewrites HTTP requests of all types. For this you can, if you want, use regular expressions.
- node-reverse-proxy — A reverse proxy which forwards incoming HTTP requests to multiple back-end HTTP servers based upon HTTP Host header.
- node-varnish — Connector for the Varnish cache telnet management protocol
- notp — NodeJS One Time Password authentication, compatible with Google Authenticator
- onion — Simple and flexible middleware stack that enables you to add a middleware layer to just about anything
- protobuf_for_node — In-process JS-to-C++ communication using protocol buffer services
- protobuf — A fork of protobuf_for_node with an npm package.
- proxy-tamper — A proxy server that allows for the tampering of requests and responses.
- socket-logger — JSON-parsable logs for Socket.IO that can push log messages to a client over Socket.IO.
- Alligator — Application Server on top of NodeJS (JSSP and SSJS support)
- Bricks — Ultra modular web framework
- Cluster — Extensible multi-core server manager (spark successor)
- Common Node — Synchronous CommonJS compatibility layer that includes JSGI, jBinary, IO, Filesystem, System and HttpClient modules
- Connect — Middleware framework packed with high quality / performance middleware
- fugue — Unicorn for Node.js - Multiple node server instance manager with Unicorn-like features
- js2 — Syntactic Sugar and Object Oriented Javascript
- JSGI-Node — Asynchronous JSGI 0.3 Adapter for Node, for standards-based middleware
- mime-magic — Proper MIME type detection library that wraps the libmagic functionality
- mob — Mob - Cluster microframework with worker process specialization
- mongrel2 — mongrel2 - Mongrel2 handler for node
- multi-node — Launch multiple node processes for HTTP servers
- Nitrode — A lightweight, super fast HTTP 1.1 with internal JS API
- node-cgi — CGI adapter kludge (replaces Node's fast and famous event-based HTTP library)
- node-digest — HTTP Digest authentication for NodeJS
- node-elf-logger — Configurable HTTP logging library following the W3C Extended Log File Format specification
- node-flags — node-flags - Flag handling library
- node-mime — Utility module for mime-type lookups
- node.ly — A simple URL shortener (currently not available on GitHub)
- Q-Oper8 — Scalable multi-process manager for Node.js
- solder — Combines and minifies JavaScript and CSS at runtime and build time
- Spark — A command-line tool used to manage node server processes
- Spark2 — Fork of Spark with some improvements.
- Stereo — A simple drop-in multi-core node application controller
- Unlimit — chaining to JavaScript without extending objects' prototypes
- what — What - A Node.JS Web Container/Application Server
- zen — Robust high performance middleware engine
- node-sqlserver — Microsoft Driver for Node.JS for SQL Server and Windows Azure SQL Database
- node-mssql — A node.js MS SQL Server native driver
- tedious — A pure Javascript implementation of the TDS protocol, for connecting to SQL Server databases
- node-tds — Pure JS client to SQL Server
- FastLegS - PostgreSQL ORM on top of node-postgres.
- node_postgres — Beginning of bindings to libpg
- node-postgres — Well tested libpq binding and pure javascript PostgreSQL client
- ORMnomnom - ORM that mimics Django's ORM, on top of brianc's node-postgres. Also supports SQLite via developmentseed's sqlite3.
- postgres-js — Fork of postgres-js, adds parameterized queries and prepared statements.
- postgres-js — Postgres protocol implemented in pure JS
- sequelize - An easy-to-use cross-database Object-Relational-Mapper (ORM) for Node.JS. Supports currently MySQL, PostgreSQL and SQLite.
- db-mysql - Binary driver for MySQL (using libmysql). Part of the Node.js DB effort
- node.dbslayer.js - Interface to DBSlayer (MySQL)
- node-handlersocket — Pure JavaScript client for HandlerSocket Plugin for MySQL
- node-mysql — A node.js module implementing the MySQL protocol
- node-mysql-pool — connection pool for node.js on top of Felix Geisendörfer's MySQL client node-mysql.
- node-mysql-cache — MySQL query cache for node.js on top of Felix Geisendörfer's MySQL client node-mysql.
- node-mysql-queues — Wraps 'node-mysql' to provide mulitple query queues, allowing support for multiple statements and MySQL transactions.
- node-mysql-native — Yet another pure JS async driver. Pipelining, prepared statements.
- node-mysql-libmysqlclient — MySQL asynchronous bindings based on libmysqlclient
- RDX - An object-relational mapper (ORM) for node. Backends: node-mysql-libmysqlclient.
- node-poormansmysql — Asynchronous MySQL driver for node.js using the mysql command-line tool
- node-mysql — Pure JavaScript MySQL async driver [obsolete]
- patio - An ORM/query engine for node (Currently only MySQL). Supports MySQL
- persist - ORM framework supporting MySQL and SQLite 3 relational databases.
- persistence.js — An object-relational mapper (ORM) for node. Backends: MySQL.
- sequelize - An easy-to-use cross-database Object-Relational-Mapper (ORM) for Node.JS. Supports currently MySQL, PostgreSQL and SQLite.
- node-sqlite — Bindings for SQLite3. Interface conforms to the HTML5 Web SQL API
- node-sqlite — Fast asynchronous driver: New evented Node.js look, same great SQLite3 taste
- node-sqlite3 — Full-featured, asynchronous SQLite bindings with query serialization/parallelization and verbose stack traces
- persist - ORM framework supporting MySQL and SQLite 3 relational databases.
- sequelize - An easy-to-use cross-database Object-Relational-Mapper (ORM) for Node.JS. Supports currently MySQL, PostgreSQL and SQLite.
- noradle — NodeJS gateway for oracle plsql server page and driver for javascript to call oracle stored plsql procedure and get result sets.
- oracle — Database driver for Oracle
- GridFS - Simple GridFS capabilities built on node-mongodb-native.
- mongo-express - Web-based MongoDB admin written with express.
- mongo-sync — A synchronous MongoDB driver for use with Common Node that attempts to closely approximate the MongoDB shell.
- mongoclikker - Simple MongoDB data viewer
- mongodb-viewer — NodeJS MongoDB web-based admin/viewer tool.
- mongojs — Simple driver that emulates the mongodb API as much as possible.
- mongolia — Flexible wrapper for the nodejs Mongo driver. Lighter than a ORM but easier to use than the driver.
- mongoose-nested-set - A mongoose plugin implementing the nested set pattern for mongoose models
- mongoose-paginate — Mongoose ORM (NodeJS) Document Query Pagination.
- mongoose — Mongoose is a JavaScript library that provides an ORM for MongoDB.
- Mongorito — Mongorito is an ODM for MongoDB. Hooks, validations, models, built-in caching. Everything you need is already there.
- mongous — Mongous is a simple MongoDB driver that uses a jQuery styled syntax.
- node-mongodb — Basic MongoDB client implementation in JS/C++
- node-mongodb-native — A pure JavaScript driver for MongoDB.
- node-mongoskin — A future layer for node-mongodb-native.
- node-mongodb-wrapper — As close as possible to the command-line driver. Uses node-mongodb-native.
- mongode — Very thin wrapper around node-mongodb-native that simplifies the API a bit.
- N-Ext — Use Ext.data packages in your NodeJS apps (includes a MongoDB proxy based on node-mongodb-native)
- mongoq — MongoDB is: mongoq('mongodb://localhost/db').collection('users').find().toArray(function(error, docs){})
- node-mongolian — Mongolian DeadBeef is an awesome Mongo DB node.js driver that attempts to closely approximate the mongodb shell
- nosql-thin - Makes easier to work with MongoDB. Not an ORM. Not production ready.
- Official driver - Vote for it on 10gen's tracker
- node-hive — dead simple hive client using thrift api
- node-thrift-hive — Hive client with multi versions support and a Readable Stream API.
- awesome — a Redis clone in node.js
- nedis — Redis server implementation written with node
- node_redis — Complete Redis client that works well with new node and new Redis
- nohm — Redis object relational mapper (ORM)
- rdb-parser — async streaming parser for Redis RDB dumps
- redback — a high-level Redis library with support for advanced data structures such as Social Graphs and Full-text Indexes.
- redis-node-client — Redis Client by Fictorial (deprecated)
- redis-node — Comprehensive, high speed Redis client
- redis2json — Easily loads data from Redis into structured JS object
- ron — Redis object relational mapper with a minimum of magic
- couch-ar — a active record implementation for couchDB (uses cradle)
- couch-client — a simple wrapper around CouchDB's http interface
- couchcmd — CouchDB command line utility using cradle
- couchtato — CouchDB document utility tool
- cradle — a high-level, caching, CouchDB client
- data.js — Graph persistence for Node.js with CouchDB
- LazyBoy — a CouchDB ORM thats easy to use
- nano — Minimalistic driver for CouchDB based on mikeals/request
- node-couch — a CouchDB connector
- node-couchdb-api — An easy-to-use and powerful wrapper for the CouchDB API that follows Node.JS conventions for async code.
- node-couchdb-min — Light-weight client with low level of abstraction and connection pooling.
- node-couchdb — A full API implementation
- PJsonCouch — PJs-on-Couch is a client lib for CouchDB's HTTP API
- Sag — Gently wraps the CouchDB API, giving you power without a bunch of "stuff". The same source file works in the browser, allowing you to code against one API everywhere.
- YACA — custom api for your couchdb instance generated through introspection.
- alfred — a fast in-process key-value store for node.js that has functional indexes, streams, replication, ...
- barricane-db — a transparent object persistence mechanism
- cassandra-node — Node.js driver for CQL and Apache Cassandra
- chaos — Chaos is a Node.js database
- dynode — A client for Amazon's DynamoDB service
- keys — Unified interface for key/value store clients
- kyoto-client — A client for the Kyoto Tycoon key-value store
- neo4j — Neo4j graph database driver for Node
- node-dynamodb — DynamoDb Driver for Node.js
- node-leveldb — NodeJS bindings to levelDB - a fast and lightweight key/value database library
- node-mwire — Client for GT.M & Cache databases
- node-tokyocabinet — Tokyo Cabinet binding
- node-tyrant — An implementation of the Tokyo Tyrant network protocol for the Node.js
- riak-js — Riak JavaScript client (works on node v0.1.30+)
- db-drizzle - Binary driver for Drizzle (using libdrizzle). Part of the Node.js DB effort
- db-migrate - Relational database migration framework
- dynamoDB — An Amazon AWS DynamoDB library for Node.js.
- hive — Fast in memory store for Node.
- JSLINQ — Clean and simple port of Microsoft's LINQ to node.js (and the browser)
- jugglingdb - ORM for every database: redis, mysql, neo4j, mongodb, ...
- LDAP - LDAP Search/Modify/Auth with Syncrepl Async update notification
- memcached — Memcached, membase client with support for memcached clusters using consistent hashing.
- Memcacher — Memcached client, based on node-memcached, adds tagging functionality to memcached, without modifying it or its source.
- nconf — A hybrid local / remote configuration storage library
- nocr-mongo — NoCR implementation for mongoDB
- node-cask — A 140 loc mmap-ed key/value store, based on bitcask
- node-dbi — A database abstraction layer, which allows to work with several database engines (MySQL, SQLite) with a single handy API
- node-dbmon — Database Real-Time Monitoring Library
- node-dirty — A key value store for node.js that is simple, fast & dirty.
- node-firebird-libfbclient — Firebird SQL client
- node-fleet — a FleetDB Client
- node-fsdocs — Simple, ACID and versioned file system-based document database for quick hacks
- node-gdbm — GNU's GDBM wrapper library for Node.
- node-hbase — HBase client with full API support through the REST connector
- node-mdb — Node.js-based clone of AWS SimpleDB, using GT.M for data storage
- node-mdbm — a client for GT.M and Cache, using an HTTP interface that is based on SimpleDB
- node-migrate — Migrate - A database agnostic migration system for Node.js
- node-odbc — unixODBC bindings for node. Query any database that has an ODBC driver.
- node-orm — ORM for multiple drivers (MySQL, PostgreSQL, MongoDB)
- node-springbase — Plug-and-play cloud relational data store for Node applications
- node-sqli — Promise-based API for interacting with SQL databases.
- node-tiny — An experimental in-process database similar to nStore.
- persist - ORM framework supporting MySQL and SQLite 3 relational databases.
- persistence — Multi-backend database/nosql system. Backends: Sqlite3, Postgres and Memory.
- perstore — JavaScript persistence/object store with pluggable storage based on the W3C DB API
- rawhash — Experimental in-memory key:value cache where keys are binary Buffers. Built on google-sparsehash and murmurhash3
- searchjs — Library for doing easy native JSON SQL-style querying on JS objects/arrays. Implements JSQL, native JSON query language.
- sequelize - An easy-to-use cross-database Object-Relational-Mapper (ORM) for Node.JS. Supports currently MySQL, PostgreSQL and SQLite.
- simpledb — An Amazon AWS SimpleDB library for Node.js that is user-friendly and fault-tolerant
- squel — Light-weight SQL query string builder (works in browser too).
- Tabler - Access relational and NoSQL database backends using a generic SQL-inspired table interface (SimpleDB, JSON file available)
- ueberDB — Transforms every database into a object key value store
- asyncEJS — Asynchronous implementation of embedded JavaScript
- bake — Templating engine for static files. Supports ejs templates.
- bind-js — a simple templating engine for node.js that smiles back.
- Blade — HTML Template Compiler, inspired by Jade & Haml. Express compliant
- bliss — Template engine inspired by .NET Razor and Play! Scala Templates.
- blue — A streamed template engine. Support for asynchronous loading of partials
- CoffeeKup — Markup as CoffeeScript. Live demo and html2coffeekup
- CoffeeMugg — Markup as CoffeeScript. A branch of CoffeeKup, supports closures, requires no compilation, extensible via view-helper libraries, like ActionView.
- combyne.js — A template engine that hopefully works the way you'd expect.
- doT.js — Concise and super fast javascript templates with customizable delimiters, streaming and partials support
- DryKup — CoffeeKup-compatible markup for CoffeeScript. Supports closures and requires no compilation.
- dust — Async, streaming & composable templates for the browser and node
- Eco — Embedded CoffeeScript templates
- ejs — Light-weight Embedded JavaScript implementation. Express compliant
- haml-js — Server side html generation using JavaScript. Parses haml templates and renders html.
- haml.js — Faster / more compliant implementation of Haml. Express support
- handlebars.js — The mustache-inspired templating library behind Ember.js
- Jade — Haml-like template engine, fantastic error reporting, easier to read, higher performance. Express compliant
- jazz — A readable template language for node.
- JinJS — A port of Jinja, a Django Template-like templating language to Javascript
- jm — Another Builder/Markaby/Erectory clone in JavaScript.
- jqtpl — A port of the new jQuery template engine. Express compliant
- jsdom — pure js implementation of the dom level 1 with some browser augmentation. Level 2 and 3 are being considered.
- jshtml — Clean HTML, full JavaScript template engine. Inspired by the razor view engine used in asp.net mvc.
- JSON Template — Minimal but powerful template language with multiple implementations. This is the CommonJS version, tested on Node
- Kiwi — Simple yet powerful template engine based on jQuery Templates syntax, totally asynchronous, and built from the ground up with performance and modularity in mind. Express 3.x compliant.
- less.js — official port of Less to JavaScript/node.
- Liquor — A templating engine minus the code.
- Lite(LiteXML) — A cross platform template engine base on xml/html and javascript expression
- minimal.js — a fast HTML+JSON template engine (both browser and Node.js compatible)
- Mu (Mustache) — A Mustache engine that compiles templates into very fast rendering functions. Also streams the
- node-dom — Javascript fast W3C DOM generation.
- node-jst — A pretty high performance template engine implemented with JavaScript. Express compliant
- node-pages — A simple template engine. Cacheable and Trackable.
- node-properties — Simple property reader, externalise your configuration through JSON
- Node-T — A fast django-like templating engine for node.js
- node-template — Fast and light cached templates.
- node-tmpl — basic templating system in node.js based on shorttag
- node.magic_dom — A DSL for building HTML in node.js, similar to Python's Stan
- nodejs-meta-templates — like php mixed with html, processed twice, 1st static data, 2nd dynamic data.
- normal-template — Normal templates are simple, yet powerful. They are safe, usable in non XML/HTML contexts and portable to any programming language.
- nTPL — Fast & Native extendable template system
- nun — Totally asynchronous non-blocking template engine for node.js
- Parrot — A lightning fast and lightweight templating engine for Node.js (Just 33 LOC!)
- Pencil — Custom tags/components for Jade. Extend, mixin and inherit.
- plates — A fast, non-intrusive engine (similar to weld, but faster).
- PURE — Unobtrusive Rendering Engine. The HTML view and the JS logic remain strictly separated. Works with jsdom.
- QEJS — Asyncronous Embedded JavaScript Templates with Q promises
- shift.js — Standard interface to the Node.js template engines.
- stencil — Renders asynchronous templates shared by server and browser
- Swig — Fast and powerful Django-style templates for node.js. Express compliant.
- Templ8 — A fast, lightweight, yet powerful & feature rich client/ server template engine based on Django style syntax
- template.node.js — A light, fast, cached template module for node.
- thunder — A lighting fast template parser for node.js. Express compliant
- tmpl-node — a feature-rich template module for node.js
- tob — Template Observer, allows self-reloading templates with your template engine
- tpl — a general purpose template cli
- TSN — A Templating System for Node.JS.
- TwigJS — A port of PHP template engine (www.twig-project.org) to Javascript
- weld — unobtrusive. portable. ANTI-TEMPLATING. Weld binds data to markup, and can generate markup based on your data.
- whiskers — single-file, feature-sparse templating for node, express, and the browser
- xmlbuilder-js — An xml builder for node.js similar to java-xmlbuilder.
- xmlbuilder.js — An xml builder in JavaScript inspired by Ruby's Builder, Markaby, and Erector. rendering process.
- carto — Parses MSS templates and renders XML for Mapnik
- ccss — Parses a JavaScript object and renders css.
- csslike — Parses and compiles CSS, taking into account the most recent www-style proposals.
- CSSOM —- Parses CSS into CSS Object Model.
- Csster —- Write CSS in JS or Coffeescript, with macros, color math, etc.
- Inverter — Flips CSS from LTR to RTL and vice-versa.
- less — Parses a LESS template and renders css.
- sass.js — Parses Sass templates and renders css.
- scss-js — Parses SCSS templates and renders css.
- Stylus-Sprite — Extension for Stylus to generate sprite images
- Stylus — Expressive, dynamic, robust CSS language written by the author of Express, Connect, Jade and more
- AE86 — Static website generator. Easy to write custom tag as simple JavaScript function, clean and minimalistic template syntax.
- blacksmith — A static site generator built with Node.js, JSDOM, and Weld.
- Calipso — An express based CMS, very much work in progress.
- DocPad — is a language agnostic document management system. It's actively maintained and supported by a growing community. It's also modular; so easy to extend and use in bigger systems. It already supports lots of markups and pre-processors through available plugins and powers many websites.
- jslardo — A Social CMS, where users are able to register to the application, create their own models (mongoose schemas), views, controllers, websites and pages. Work in progress, keep updated!
- LooseLeaf — Very simple blogging engine based on express, no-DataBase, only JSON.
- MCMS — A Minimal CMS using GitHub flavored Markdown, Mustache templates and the filesystem.
- MuContent — MuContent is a multisite and multilanguage cms in Javascript (Node.js) written with a central proxy for balancing the request on multiple client. Based on MongoDB for storage all content. Themes are based on Foundation framework and mustache.
- Murray CMS — blog platform using express and mongodb.
- NoCR — JCR-inspired Content Repository API for Node.js which maps your content model to a tree of nodes and properties. There is a MongoDB NoCR provider available
- Nodepad — A nice basic web-based notepad.
- reed — A Markdown-based blogging core backed by Redis and the filesystem.
- asereje — Bundles and minifies your javascript and css files on demand. Forget about builds, hard deploys and cache forever.
- asset-bundler — pack and create asset bundles, organize your scripts and stylesheets in different packages.
- buildr — JavaScript project builder, compresses images, CSS and JS, bundles CSS and JS, checks JavaScript, supports subpackages/plugins
- Grunt — a task-based command line build tool for JavaScript projects.
- Jake — JavaScript build tool similar to Make or Rake.
- Masson — Build system and rule management similar to tools like Make, Ant or Rake
- mnm — Make Node Module (MNM) is a build tool for making native Node.js modules with no dependencies other than Node itself (i.e. no Python required)
- muffin.js — CoffeeScript compilation, minification, concatenation, copying, growl, and SLOC counting helpers for Cakefiles.
- nib.js — package and minify JavaScript/coffeescript projects to be embedded in the browser as a single .js file
- node-linter — generic lint wrapper for JSLint, closure-linter, closure-compiler, and JavaScript Lint.
- weber — dynamically compiling and minifying scripts, stylesheets and templates on-the-fly, including Coffeescript, Eco, Stylus, etc.
- always — A CLI & Daemon tool to run a NodeJS process forever, restarting on file changes & crashes with piping to stdout or log files.
- autorestart — It is a way to watch all .js files if they have been changed and to restart nodejs. It allows easy development and stable production.
- dev — Automatically restarts the app when a source-file is modified. Autohooks on new files, so it doesn't need manual restart at all.
- forever — A simple CLI tool for ensuring that a given script runs continuously (i.e. forever)
- forever-webui — A simple web UI for efficient nodejs administration
- forewoman — A port of foreman (process management tool) + hot code reload.
- node-dev — Automatically restarts node when a source-file is modified. Displays notifications via Growl.
- nodemon — Monitors all source files, restarts node on changes. Command-line usage, code-passive with ignore list.
- reloader — Reload app on it's source code change. Suitable to work both on production and development machines. Act as usual NodeJS module.
- run — Rerun your js file whenever there's a change in the current directory: npm install -g run, runjs yourcode.js. Never alt-tab to your terminal again.
- start-stop-daemon — An 1-function Node.js module to easily create native child_process.fork start-stop-daemon scripts. Created daemons are self-monitored and restart automatically when crashing (a custom crash handler can be attached too).
- winser — Run Node.js applications as services on Microsoft Windows.
- connect-girror — Connect middleware which mounts an entire app from any git repository, spawns it and exposes a git post-receive hook endpoint.
- girror — Mirrors (and re-mirrors) git repositories.
- Gittyup — Application deployment library for node.js
- haibu — a node.js application server - spawn your own node.js clouds, on your own hardware
- jitsu — Flawless command line deployment of your Node.js apps to the cloud
- npkg — Generates cross-platform installers for Node.js applications
- roco — Capistrano inspired CLI for deployment, monitoring and other stuff.
- coffee-conf — Write your config files in coffee-script.
- config — Configuration control for production node deployments - npm install config
- configme — Simplest possible configuration tool. without conflict - with defaulting!
- envious — environment variable configuration that's too easy to pass up
- node-config — Lightweight configuration engine for Node.js
- node-ini — A simple .ini config parser that supports sections, comments, arrays, and objects.
- node-settings — Simple, hierarchical environment-based app settings.
- osenv — Gets environment settings of the operating system.
- ripple — Dynamic runtime configuration for node.js deployment based on lorenwest's config.
See also parsers, they may be used to parse configuration files.
- coffee-toaster — Minimalist dependency management system for coffee-script.
- doc.md — A simple JSDoc documenation tool that creates markdown for node.js modules
- modul8 — Extensible CommonJS browser code sharing
- nclosure — Compiler, Style Checker, Utility Library, Unit Testing Framework, JSDoc Documentation Tool. Built on top of Google Closure tools.
- octoploy — GitHub post-receive hook URL handler (execute script on push to repo)
- poly.shell — distributed shell job control with role based configuration
- quickcheck — Port of the Haskell QuickCheck unit testing framework
- ready.js — continuous javascript integration
- Cupboard — Reverse package manager
- Ender — A package manager built on NPM bringing micro to macro to create your own custom JavaScript library by composing modules into a cohesive and familiar interface .
- nmod — nmod is a node_modules manager. able to install from npm and git
- npkg — Generates cross-platform installers/executables for Node.js applications
- npm — A node package manager that uses CommonJS-compatible package.json files, written in asynchronous JavaScript.
- police — A module dependency version policing tool. It goes through all your repositories on github which has package.json and analyzes the dependencies and reports back to you about all the outdated packages.
- Slugr — Wraps node.js apps into a single executable file.
- a3 — a3 module loads any folder of code into an 'API Tree'
- directory — require a whole directory
- haba — plugin library
- node-DJs - Restart a server after each change in main script and dependencies
- node-hot-reload - watchFile and reload modules dynamically for node.js (very useful for development, less good for production)
- nodules — Async URL CommonJS module loader with dep resolution/downloading and hot-module reloading
- remap - reroute require for mocks, wrappers, or shenanigans (useful for testing)
- sourcemint-platform-nodejs — sourcemint-loader-js (optimized CommonJS package mappings based JavaScript module loader) for NodeJS
- amir's node-base62 — C++ base62 lib for representing big numbers
- brainfucker's node-base64 — C++ base64 lib
- hashlib — Fast hashing module, written in C/C++, supports: md4, md5, md6, sha, sha1, sha256, sha512
- jsHashes — A hash algorithm independent library purely JavaScript implemented for both server and client side. Support: MD5, SHA1, SHA256, SHA512, RIPEMD-160, HMAC including Base64 encode/decode, CRC-32 and full UTF-8 support.
- keygrip — Key signing and verification for rotated credentials
- MD5 — Plain JavaScript MD5 hashing function
- murmurhash3 — Node.js bindings for MurmurHash3
- node_nibbler — Base32/base64 encoder/decoder.
- node-cityhash — NodeJS bindings for Google CityHash , both CityHash64 and CityHash128 are supported.
- node-crypto — OpenSSL based Hashing, Signing and Verifying
- node-gpg
- node-hashring — Hash ring provides consistent hashing based on the libketema library.
- node-oauth — OAuth client (1 and 2)
- node-oauth2-provider — OAuth 2 provider as Connect/Express middleware with custom token storage hooks
- node-openid — OpenID 1.1/2.0 Relying Party (client)
- node-phpass — A pure JavaScript port of the portable PHP password hashing framework.
- node-sechash — Secure password hashing using salt and key stretching.
- node-whirlpool — C/C++ mhash lib wrapper providing whirlpool hash support
- node.bcrypt.js — C/C++ bcrypt lib
- NodeJS-Keychain — A security-oriented keychain web service
- oauth-server — OAuth server (1.0A)
- oauthjs — OAuth client
- packnode — Obfuscate, minify and/or encrypt JS files
- pass — Validate/generate Apache htpasswd passwords (for Basic Auth)
- pkrumins's node-base64 — C++ base64 lib that actually works
- rbytes — Secure random byte generator for session keys, UUIDs, etc.
- S3ncryptedProxy — A simple proxy that makes securely sharing data via Amazon S3 easy
- sasljs — Gsasl wrapper to performs server-side SASL authentication.
- sha1 — Plain JavaScript SHA-1 hashing function
- speakeasy — Easy two-factor authentication. Time-based or counter-based one-time passwords with the HMAC One-Time Password algorithms. Supports Google Authenticator.
- ursa — A clean and reasonably complete set of wrappers for OpenSSL's RSA functionality.
- emailjs - send emails, html and attachments from node.js to any smtp server (ex: gmail)
- Haraka — Full Featured SMTP Server
- mailcomposer - Generate e-mail messages that can be streamed to SMTP or file (unicode friendly)
- Mailman — Send emails in a comfortable way via models. SMTP, SES, Sendmail supported.
- node_mailer — send emails from node.js to an smtp server, simple as cake
- node-smtp — Implementation of an SMTP server (and soon, client)
- node-smtpevent — Event based SMTP server
- Nodemailer - Easy to use module to send e-mails with Node.JS, unicode friendly
simplesmtp - Use SMTP as a first class protocol, useful for generating custom SMTP servers
No Longer Maintained
- open-uri — A very simple HTTP(S)/FTP client library similar to Rubys Open-URI lib.
- curler — Native c++ node.js module for asynchronous http requests via libcurl.
- fetch — Fetch urls with ease, supports gzip content, cookies and more
- http-get — Simple to use node.js HTTP / HTTPS client for downloading remote files. Supports transparent gzip decoding.
- http-sync — Synchronous http(s) client
- multiparter — multipart/form-data POST request maker for Node.js with support for streams (sending files) and plain params
- multipost — An easy interface for the multipart/form-data protocol
- needle — Lightweight HTTP client with multipart support.
- node-digest — HTTP Digest authentication for NodeJS
- node-get — Moderately higher-level HTTP client library.
- node-http-status — Interact with HTTP status code (just a set of constants)
- node-httpclient — Node HTTP Client (gzip, https, cookies etc.)
- node-socksified — HTTP SOCKS5 proxy support
- node-tunnel — HTTP/HTTPS Agents for tunneling proxies.
- poster — Upload local/remote files over multipart.
- request — Simple HTTP client library.
- shred — HTTP client library for Node.js and browsers. Supports gzip, cookies, https, proxies, and redirects.
- superagent — High-level HTTP client sporting a progressive API
- watchmen — A simple HTTP monitor (pings sites and services with predefined parameters to make sure they are alive)
- ftp-get — Simple to use node.js FTP client for downloading remote files
- jsftp — A sane, light and concise client implementation of the FTP protocol
- node-ftp — An FTP client module for node.js
- node-ftpclient — Node FTP Client
- NodeFTPd — Node FTP Server ... updated fork here
- inbox — Super easy access to IMAP mail server mailboxes
- n3 — Experimental POP3 server to send arbitrary data to e-mail clients (including e-mails)
- node-imap — A module for interacting with IMAP mail servers
- node-poplib — POP3 client library for Node.js
- jsDAV — jsDAV allows you to easily add WebDAV and CalDAV support to a NodeJS application. jsDAV is meant to cover the entire standard, and attempts to allow integration using an easy to understand API.
- macaddr — Obtain MAC addresses for current machine from Node
- mdns — mdns/zeroconf/bonjour service discovery add-on
- ndns — DNS client/server library
- node-hydna — Bindings for the Hydna platform
- node-icecast-stack — An interface for connecting to, parsing metadata, and reading from SHOUTcast/Icecast radio streams
- node-nntp — An NNTP (usenet/newsgroups) client module for node.js
- node-ping — Simple wrapper around fping
- node-snmp — SNMP v1 client
- node-snpp — Node SNPP server library
- node-ssltunnel — A lightweight TCP over SSL / TLS tunnel running over node. If you need to add confidentiality, integrity, and authenticity to your TCP stream this is the tool for you.
- node-tld — for working with TLD data (registered domain name, etc)
- NodeSSH — a lightweight SSH client
- portscanner — An asynchronous port scanner for Node.js
- radius — RADIUS packet encoding/decoding library
- shorty — Shorty is a lightweight, high performance SMPP client and server.
- stomp-client — A STOMP client for Node.js
- wake_on_lan — Generate and send Wake-on-LAN magic packets
- bertrpc
- dnode — Asynchronous remote method calls with transparently wrapped callbacks. Works with regular network streams or socket.io.
- IPCNode — Asynchronous RPC library based on node.js Stream object, with support for circular objects, and explicit reference counting to ensure proper garbage collection.
- jsonrpc
- jsonrpc2 — A super easy to use JSON-RPC v2 server
- messenger — Dead Simple API for cross-service communication (supports Pub/Sub, Request/Reply, Fire and Forget models).
- msgpack-rpc — Implementation of Msgpack-RPC (http://msgpack.org)
- node-ipcbuffer — A modified Buffer object to pass large amounts of data between processes using POSIX or System V IPC shared memory and or do fast read and writes to files. (Windows soon).
- node-jsonrpc — Another JSON-RPC server
- node-pingback — Pingbacks for node.js, conforming to the pingback and xml-rpc spec.
- node-soap — SOAP client and service (partial support).
- nodejs-light_rpc — Simple server/client RPC, with minimal dependencies (uuid (can be removed to use simple increment)).
- nodeQuery — nQuery.js lets you create, edit, update or delete the DOM in real-time using jQuery/Zepto style code written on the server
- nowjs — nowjs makes realtime web apps really easy (http://nowjs.com)
- rpc-socket — Multi socket support for JSON-RPC NOTE: still under development.
- thintalk — A minimal extendable RPC abstraction with buildin TPC and IPC support
- xmlrpc — A pure JavaScript XML-RPC server and client
- zeromq.node — 0MQ (zeroMQ) bindings for node.js
- Abstract HTTP Request — An HTTP Client wrapper for the browser (XHR) and node.js (http module)
- Beseda — Fast, well designed and featured Node.js Pub/Sub server. Beseda offers multiple platform API to develop realtime web applications.
- Comet LongPollingBuffer — A Library to simplify the server side of Comet AJAX long polling
- easywebsocket — WebSocket to broadcast messages to webpages
- Faye — Bayeux protocol Comet client and server for Node.js and Rack
- grappler — A minimalistic server for handling "comet" connections that supports a variety of connection methods.
- Minotaur — Long poll server using JSONP communication with clients
- node-bus — A distributed pubsub engine for JSON-based events
- node-eventstream — A server-side companion to EventSource.
- node-object-sync — Transparently synchronize objects across connected clients
- node-rpc-socket.io — socket.io addon, add a full client/server implementation of JSON-RPC
- node-socket.io-client — Node.js implementation of the Socket.IO client libraries
- node-websocket-client — An HTML5 Web Socket client.
- node-websocket-server — Another websocket server on top of the http server.
- node-wwwdude — A simplified, flexible HTTP/REST client library for node.js
- node-XMLHttpRequest
- node.websocket.js — WebSocket-compatible server.
- node.ws.js — A basic Web Socket server with interface similar to tcp.createServer(...)
- node2node-socket.io — A node2node transport and client for socket.io .
- nodejs-http-websocket — A websocket server on top of the http server.
- Restler — Simplified REST client for Node.js
- Reston — REST client with multipart support and friendly API
- Socket.io — WebSocket-compatible server and client with fallback for legacy browsers
- spacesocket — WebSocket server not invented here
- tunguska — A comet publish/subscribe distributed hub (runs across multiple node instances).
- wave-node — An implementation of the Google Wave Gadget API for node.js
- Weasel — a command based websocket application framework
- websocket-node — A WebSocket library that implements the most current protocol versions, 8 and 13. Version 13 is the latest version that is now the final WebSocket spec as RFC 6455
- ws-flash-client — Replaces the WebSocket object with a Flash implementation on clients without native WebSocket support, thus covering 99% of use cases. Works well with the ws WebSocket server.
- ws-rpc — Lightweight RPC support for the ws WebSocket server. Supports rooms (channels), callbacks, auto-reconnection, using client WebSocket from Node.js (not only Browser), and works well with ws-flash-client.
- ws — Very fast, protocol conformant WebSocket client, server and console. Supports RFC 6455, the Hybi drafts as well as Hixie-76.
- amqp-dsl — Fluent Interface for dealing with AMQP (RabbitMQ,...) on NodeJS
- amqp-tool — Import & export data from/to an AMQP/RabbitMQ broker
- fairy — a queue engine based on Redis offering ActiveMQ's message groups alike feature.
- gearman-node — Gearman client.
- gearnode — Gearman client and worker.
- node-gearman — Simple worker/client module for Gearman
- node-rqueue — Implementation of RQueue, includes Worker and Queue
- QDis — a simple durable fanout pub/sub queueing system built with Redis + Node.js
- Que — One model-based interface to many message queue backends.
- rabbit.js — Idiomatic messaging using RabbitMQ from node.js
- rabbitmq-nodejs-client — rabbitmq client for node.js
- websocket.MQ — Reliable message queue server accessible via websockets, socket.IO, etc
- Wormhole — Fast/High Performance message queue system using streaming deserialization with the MessagePack format.
- Bike — Class system to organize namespaces, define, create, extend, mixin, super inheritance and more.
- class-js — Simple, Light-weight OO Class factory
- comb — Library with a built in OO class system, and other features(datastructures, array string and date utilities, etc..).
- ease.js — Classical Object-Oriented framework for JavaScript
- joose — complete modern class system for JavaScript, based on concepts from many programming languages
- mootools.js — MooTools latest server library as npm package for node.js
- N-Ext — Use the power of the Ext.data packages (from the ExtJS framework) in your NodeJS apps. Includes a MongoDB proxy based on node-mongodb-native)
- Sslac — Basic OOP-like support in JavaScript using chaining. Supports extension, interfaces, namespacing, and static objects.
- Structr — Library built to give JavaScript the same look and feel as other popular languages such as Java.
- UberClass — A class framework based on John Resig's Simple JavaScript inheritance and JavaScriptMVC $.Class.
- UberProto — JavaScript object inheritance sugar: Initialization methods, easy extension, mixins, super methods, proxies
- API Easy — Fluent (chainable) syntax for generating Vows tests against any RESTful API.
- benchmark-pages — A benchmarking library for your web service that measures page response time under different loads.
- Benchmark.js — A benchmarking library that works on nearly all JavaScript platforms, supports high-resolution timers, and returns statistically significant results.
- Broke — Customizable vowsjs layer for flexible unit and integration tests.
- Cucumber — The official JavaScript implementation of the well-known BDD tool. Runs both on Node.js and browsers.
- cucumis — A cucumber nodejs implementation. Run plain text gherkin stories with full asynchronous support in native node.js JavaScript.
- Cup of Tea? — BBD-style Unit Testing for Async apps.
- databasecleaner — Clean your database after each test. Supports MongoDB, Redis and CouchDB. Will support MySQL and others.
- ensure — nodejs testing made easy with vows or node-tap
- espionage — A mock/stub framework using the test spy pattern.
- exemplor.js — A port of exemplor with Node goodness.
- expresso — TDD framework by the author of JSpec
- fakeredis — Simulated Redis instances, so that you can run any number of redis-dependent tests in parallel.
- fakeweb — A port of fakeweb that fakes out requests made via Mikeal's request module, or the standard HTTP module.
- foounit — client/server side BDD testing framework
- Gently — A node.js module that helps with mocking and behavior verification.
- Gerbil — Gerbil attemps to be an uber simple and minimalistic testing framework for javascript.
- gherkin — A fast Gherkin parser in Ragel (The parser behind Cucumber)
- grover — PhantomJS/YUITest CLI test runner.
- horaa — Mocking NodeJS Modules
- httpmock — A RESTful web API for stubbing out network dependencies.
- jasmine-jquery — jQuery matchers and fixture loader for Jasmine framework for node forked from Jasmine-Jquery
- jasmine-node — integration with Pivotal's Jasmine Spec framework
- jessie — Node runner for Jasmine JavaScript BDD testing framework
- Jody — A descriptive BDD Testing framework
- jspec — Feature Rich BDD Testing Framework (no longer supported)
- JUTE — A JS testing environment with built in code coverage support for Capture/Selenium/V8 backends
- kin - Object creator using blueprints
- kyuri — A node.js cucumber implementation with a few extra asynchronous keywords. supports 160+ languages and exports to VowsJS stubs
- maryjane — Mock object library inspired by Mockito
- minitest.js — Light-weight & simple testing framework designed specially for testing asynchronous code.
- mjsunit.runner — Command line mjsunit runner which provides an easy way to hook into mjsunit and start running tests immediately.
- mocha-cakes — BDD mocha test framework add-on, stories with Cucumber style Given/When/Then syntax for Node.js
- mocha — simple, flexible, fun javascript test framework for node.js & the browser
- mockery — Simplifying the use of mocks with Node.js
- nock — HTTP mocking and expectations library.
- node-assert-extras — Additional high level asserts
- node-assertthat — Provides a fluent TDD style for Node.js: assert.that(actual, is.equalTo(expected));
- node-async-testing — Simple, fast, extendable unit testing.
- node-qunit — QUnit port for nodejs. Very simple API, async testing, good tested testing framework.
- node-replay — When API testing slows you down: record and replay HTTP responses like a boss.
- node-stories — Given/When/Then integration awesomeness for Node.
- node-testy — Super simple testing script. No added sugar. 100~ LOC.
- nodemock — Simple Yet Powerful Mocking Framework for NodeJs
- nodeunit — Simple syntax, powerful tools. Based on the assert module. Available for node.js and the browser!
- patr — Promise-based asynchronous test runner: lightweight & simple.
- pit — Simple drop-in test runner with optional control tools
- platoon — A javascript testing framework whose goals are to work gracefully with callbacks, both in node.js and the browser
- pretendr — Yet another mocking function. Simple and lightweight, it lets you mock objects just by passing them as a single argument.
- reut — Heavyweight unit testing framework.
- rewire — Dependency injection for node.js applications
- should — expressive test framework agnostic BDD-style assertions
- sinon — JavaScript test spies, stubs and mocks for Node.js and the browser
- soda-runner — Provides a command line interface, runner, and selenium ide adapter for for soda.
- Soda — Selenium Node.JS adapter
- spectacular — for testing
- speculum — BDD Test Suite.
- Speks — A specification framework for your node-code
- stest — A sane event driven async testing framework.
- supertest — HTTP test utility built on top of superagent, works with any test framework
- Syringe — Dependency mocking for node modules
- tbd — A data generator for tests & UI stubbing
- TestIt — Light-weight yet complete, shoulda style testing framework for in-browser and node.js tests.
- TestNode — Unobtrusive BDD-style testing framework
- testosterone — Virile testing for http servers or any nodejs application.
- Tobi — Expressive server-side functional testing with jQuery.
- twerp — Simple test framework which is suited to Coffeescript users. Synchronous, simple and easy.
- uubench — A tiny asynchronous JavaScript benchmarking library
- vbench — tiny visual benchmarking library powered by uubench and node-canvas
- Vows — asynchronous behaviour-driven development for node.js
- whiskey — A simple test runner for NodeJS applications.
- yeti — The YUI Easy Testing Interface: run browser JS unit tests from the command line!
- zap — a tiny testing tool for node.js
- zombie.js — Insanely fast, full-stack, headless testing using node.js
- Amanda — JSON Schema validator
- benejson — Includes pure JavaScript incremental JSON parser
- json-diff — structured colored diff for JSON files
- json-streams — Streamed JSON parser and stringifier
- JSON.js — easy to use but synchronous, built-in to V8 (no need to @require@)
- json2json — Transform (reformat) JSON from one structure to another using JavaScript
- jsonsp — JSON stream parser for Node.js.
- node-yajl — SAX-like evented JSON stream parser using yajl version 2 (fork of yajl-js)
- props — Parse json or yaml from the beginning of text files.
- yajl-js — SAX-like evented JSON parsing using yajl
- dom-js — A pure JS XML DOM based on sax-js
- libxml-to-js — XML to JavaScript object converter based on libxmljs.
- libxmljs-easy — Work with libmxmljs DOM as with plain Javascript objects, similar to E4X
- libxmljs — Bindings to libxml2
- node-expat — Fast SAX parser binding to expat
- node-expat — a fork of Fast SAX parser binding to expat, also includes XML to JavaScript object converter
- node-halfstreamxml — sax-js based, filter out nodes (with attributes and child nodes) from an xml stream by name
- node-o3-fastxml — W3C Standards based XML DOM based on fastest xml parser in the world pugiXML
- node-o3-xml — W3C Standard based XML DOM with XPath and namespaces. Built on libxml2
- node-plist — Apple Plist parser for NodeJS. Convert a Plist file or string into a native JS object.
- node-xml — An xml parser for node.js. (The github page for this module is showing some severe performance issues. Not recommended as an "out of the box" solution.)
- node-xml2js — Simple XML to JavaScript object converter.
- sax-js — SAX-like parser in pure JS
- xml-events — XML stream parser, based on node-expat. Fires contextual events when it finds a descendant.
- xml-stream — XML stream parser and editor, based on node-expat.
- xml2json — Simple SAX based XML to JSON parser. It uses node-expat.
- xmldom — A PURE JS W3C Standard based DOMParser and XMLSerializer (DOM Level2 CORE).
- aparser — Async ARGS parser
- argparse — One of the most featured & flexible parser. Direct port of python's argparse module
- argsparser — A tiny limited arguments parser, returns a hash.
- argumentr — simple to use but powerful argument parser with free --help
- celeri.js — Complete command line interfaces lib for node.js
- cli — Rapidly build command line apps. Full featured opts/args parser + plugin support
- COA — Command-Option-Argument: full featured command line parsing, async all the way, use user defined COA's as module with API, auto generate shells completions
- commander.js — The complete solution for nodejs command-line interfaces
- dreamopt — Command-line parser with readable syntax from your sweetest dreams
- js-opts — Another simple command line option parser, easily installed via NPM
- mingy — Command parser for CLI tool and adventure game needs. Works well with the optimist module.
- node-arguments — Yet another simple command line option parser
- nomnom — Option parser with generated usage, support for callbacks and commands.
- nopt — The command line options parser npm uses.
- Operetta — A Node Option Parser That Sings!
- optimist — Light-weight option parsing without optstrings or any of that silliness. It's just a hash!
- options.js — An inline (non-module!) options parser that is very small and yet quite complete.
- optparse-js — Option Parser in JS
- parseopt — Flexible and extensible option parser in JavaScript.
- tav — Brain-free command-line options parser for node.js
- trollopjs — Another option parser
- yanop — Yet Another Node Option Parser
- node-csv-parser — Full featured CSV parser with simple api and tested against large datasets.
- node-csv — Efficient Evented CSV Parsing.
- ya-csv — Evented CSV parser and writer with configurable separator, escape and quote characters
- highlight — Code highlighter for Markdown code blocks and for automatic language detection
- marked - A markdown parser and compiler. Built for speed.
- node-discount — C markdown parser "discount" bindings
- node-markdown — Easy to use Markdown parser
- Robotskirt — A simple node binding for the sundown markdown parser
- JS-YAML — Full featured YAML parser. Native PyYAML port.
- props — Parse json or yaml from the beginning of text files.
- yamlish — Minimally featured YAML parser which handles the subset of YAML used by TAP.
- yamlparser — A JavaScript YAML parser.
- yamlprompt — A prompt that asks for YAML input. Depends on yamlparser.
- bufferlist — Create linked lists of buffers and asynchronously parse binary data from these lists
- butter — Butter === nodeJS Buffer + ( some hexadecimals delights )
- epub — Parse EPUB electronic book files with Node.JS, load chapters, images etc.
- jParser — Generates parsers for arbitrary data structures, parses binary files.
- jspack — JavaScript library to pack ints, floats, etc. to octet arrays representing C data structures
- musicmetadata — Get music metadata asynchronously from node streams.
- protobuf_for_node — Protocol buffer parsing and serialization.
- protobuf — A fork of protobuf_for_node with an npm package.
- reified — JS Binary Data API. Structs, arrays, bitfields, and data. Reify and Reference like nobody's business.
- strtok — A streaming tokenizer library for binary data
- struct — Parse/format binary data in buffers.
- feed-tables — a lightweight parser for Google Spreadsheet tables available as cells or list atom feeds.
- feedme.js — RSS/Atom/JSON feed parser with streaming capabilities.
- feedparser — RSS, Atom, and RDF feed parsing in Node.js
- NodePie — Simple RSS/Atom parser for Node.JS
- rdf2json — A simple RDF/XML to JSON converter
- VIE — RDFa parsing in Node.js
- 2kenizer — It streams, it caches and it's simple, it matches RegExp's and strings.
- bnf — BNF parser, compiler, and interpreter framework.
- canopy — PEG parser compiler for JavaScript.
- jison — A parser generator written in JavaScript; similar to Bison for C.
- jparse — A parser combinator for JavaScript based on Packrat parsers and Parsing expression grammars.
- JSCC-NODE — The Best and The first parser generator written in JavaScript; Use a regular expression-based lexical analyzer generator and a LALR parser generator for building a stand-alone, working parser.
- language.js — A fast PEG parser generator with first class errors and reasonable sized output.
- OMeta JavaScript compiler
- PanPG — A PEG Packrat Parser Generator for JavaScript.
- parser — A general purpose parser that feeds on tokens (see tokenizer).
- PEG.js — Parser Generator for JavaScript.
- ReParse — A parser combinator library for JavaScript like Haskell's Parsec.
- tokenizer — A regex-based and streamed tokenizer.
- apricot — Hpricot inspired clone, JSDom with Sizzle Selector engine and XUI for HTML augmentation
- argtypes — Function arguments type parser (Allows typed & optional arguments)
- carrier — Evented stream line reader for node.js
- fastcgi-parser — FastCGI protocol parser and Writer
- glossy — Syslog message parser and producer
- groan — A PHP session file parser
- lastname — Parse text and search persons by names and surnames. Only at russian for now.
- mailparser — Parse mime encoded e-mails into structured objects
- memcache-parser — Memcached binary protocol parser
- node_spreadsheet — Read xls,xlsx,ods,csv spreadsheets (php based)
- node-brainfuck — Brainfuck interpreter
- node-browscap — browscap.ini parser and port of PHP's get_browser function
- node-crawler — Web crawler/spider with JSDom, jQuery & connection pooling.
- node-formidable — A node.js module for parsing form data, especially file uploads.
- node-htmlparser — Forgiving HTML Parser in JS for both Node and Browsers
- node-msgpack — Bindings for MessagePack, space-efficient object serialization library
- node-netstring — Read and write netstrings
- node-opds-parser — OPDS Catalog Feed Parser for node.
- node-opmlparser — OPML parsing in Node.js
- node-properties-parser — A parser for .properties files written in javascript.
- node-rfb — Parse the RFB protocol used by VNC
- node-tldtools — Provides domain enquiry features, whois, TLD token extraction etc.
- node-useragent — User agent string parser with accurate browser versioning and grouping
- omcc — A command line tool for Alessandro Warth's OMetaJS
- ometa-js , ometajs — A JavaScript OMeta implementation
- parser_email — Simple multi type email parser
- pcap-parser — Packet capture (pcap) file parser written in pure javascript
- pdc — A pandoc wrapper for node.
- picksy — Extracts the relevant text from a html article page
- Platform.js — A platform detection and user agent parsing library that works on nearly all JavaScript platforms
- properties — Provides a simple way for persisting key-value properties.
- querystring.node.js — Robust query string parsing for node.
- ret.js — A regexp tokenizer.
- semver — Parser used by npm for version numbers.
- service-parser — /etc/services file parser
- strscan — Simple string tokenizer for lexical scanning operations
- tap — Tools for the Test Anything Protocol
- tldextract — Extract domain, subdomain and tld from a url.
- UglifyJS — JavaScript parser and compressor/beautifier
- buffered-reader — Fully configurable buffered reader.
- buffered-writer — Fully configurable buffered writer.
- diskspace.js — A simple module to check disk space usages in bytes.
- file-utils — File and directory utilities. Object representation of file and directory path names.
- fs-extra — Patches the Node.js fs object with a couple of extra methods (a method to copy a file and a method to delete a directory recursively), both sync and async versions.
- fstream — High-level objects like FS streams, but with stat on them, and supporting directories and symbolic links, as well as normal files. Also, you can use this to set the stats on a file, even if you don't change its contents, or to create a symlink, etc.
- fsutil and fsautil — Synchronous and asynchronous file system utilities for Node.js.
- hdfs — JNI-implemented HDFS client allowing node.js applications to natively interoperate with the Hadoop FileSystem.
- line-reader — Asynchronous line-by-line file reader.
- meta-fs — Patches the Node.js fs object with methods that find files, delete directory trees, create nested directories, make symlinks, copy files and directories.
- pathspec — Shell- and .gitignore/.npmignore-style wildcards and file lists
- wrench — Recursive operations (create directory, delete directory, read contents, chmod, chown, deep copy). Line-by-line reader. Asynchronous read of directories contents.
- autolint — Autolint watches your files for jslint/jshint-errors.
- debug — node core style debugging for your libraries and applications
- Eclipse debugger plugin — Using Eclipse as Node Applications Debugger
- eyes.js — A better, customizable value inspector for node.js
- http-console — A simple & intuitive HTTP console for testing APIs.
- jsdev — a wrapper for Douglas's JSDev
- monitor — Runtime monitoring for node.js applications - npm install monitor
- ndb — traditional command line debugger (like gdb, ruby-debug, etc)
- node_debug — HTTP based console and object explorer
- node-codein — browser based console and object explorer (stand-alone, no dependencies)
- node-custom-debug — debugger on custom port on the fly, good addition for node-inspector to inspect many instances on the one server with ease
- node-inspector — browser based node debugger
- Node-JavaScript-Preprocessor — Preprocessor for JavaScript
- node-profiler — access the V8 profiler from node.js
- node-webkit-agent — WebKit devtools agent that leverages remote debugging and profiling of nodejs applications using built-in webkit devtools front-end.
- nodetime — performance profiler for node.js
- profilejs — profiling middleware for use with node-inspector (profiles Express handlers)
- Replica — Repl to exec JavaScript code on browser
- spine — Dynamic runtime monitoring for node.js applications inspired by lorenwest's monitor.
- strack — bug tracking/tickets system
- UltraREPL — Replaces Node's built-in REPL with customizable keybindings, a significantly enhanced inspector with hidden/builtin/depth toggles, simultaneous independent V8 contexts, syntax highlighting, and a full-fledged plug-in system including CoffeeScript contexts, SpiderMonkey contexts, and more.
- v8-profiler — v8 profiler bindings with node-inspector integration
- webrepl — Serve a repl for a node process via a simple web ui
- ain — Brain-free syslog logging
- Caterpillar — Caterpillar is an awesome, simple, intuitive console logger for node.js. It supports grouping of messages, filtering log levels, colors, times, modules, custom formatters and custom transports.
- cfdump.js — a more visually inspiring way to dump your objects to a browser
- flume-rpc — A flume-compatible (RPC) logging source and sink
- hexy.js — hex pretty printing like xxd
- inspect — hierarchical object inspection displaying ancestry, properties, methods, and accessors in great detail.
- jog — JSON-based logger with multiple stores, namespacing, CLI and more
- log.js — light-weight logger that works with any writable stream, also supports a streaming parser for reading the log files generated
- log4js-node — a port of log4js framework for node.js
- logging — Super sexy color console logging.
- logly — A minimal logging utility to support verbose and debug modes with basic colors
- logule — A sexy portable logging utility using colors
- nlogger — Logging lib that can print also module names and line numbers, optionally in color
- node-logentries — A winston-compatible wrapper library for the logentries.com service
- node-logging — Simple colorized logging for Node.js with request logger Express middleware
- node-streamlogger — Extensively customizable logging, with support for rotation
- node-tick — v8.log processor
- NodeLog — Logging library for Node.js based on the java.util.logging library.
- nogg — Simple file/stdio logging for node.js
- spruce — configurable node.js logging module
- tracer — A powerful and customizable logging library for node.js. support color console with timestamp, line number, method name, file name and call stack. you can set transport to file, stream, database(ex: mongodb and clouddb, simpledb). keywords: log, logger, trace
- underscore.logger — Cross-browser and Node.js empowered logging that can be used as an Underscore mixin.
- Windows Event Log Js — native Node.js module to log messages to the Microsoft Windows EventLog
- Winston — A multi-transport async logging library for node.js
- ansi pansi — basic ANSI formatting, foreground and background colours for use with CLI
- ansi-font — ANSI font styling (background and foreground colours)
- ansi.js — advanced ANSI formatting tool for Node.js
- ansidiff — a small node.js library for ANSI colored text diffs
- ANSIdom — a quick and dirty DOM implementation in ANSI escape codes
- ansimator — simple animations onto the terminal with ANSI codes
- ANSInception — Colorful exception handler for Node.js with CoffeeScript support and improved nodemon/supervisor compatibility
- ansiparse — parses text with ANSI codes to an array of JavaScript objects
- ansispan — changes ANSI color codes to HTML <span> elements with CSS color styles
- charm — use ansi terminal characters to write colors and cursor positions
- cli-chart — creates pseudographic charts in Node console with ANSI-coloured blocks
- cli-color — colors and formatting for the console
- colored.js — Colorize terminal output.
- colorize — Markup tool which uses English words to set text colours with ANSI codes.
- colors.js — get colors in your node.js console like what
- colours.js — Give your CLI programs some colours! (Does not support background colour manipulation.)
- consolelog.js — stylized console logging for node.js
- irc-colors.js — Color and formatting for irc bots made easy
- kahve-ansi — easy-mode insertion of ANSI colour codes
- node-cli — cli.clear().move(20,20).color('green').write('Node-cli').down(1).back(7).color('yellow').write('Rocks!')
- prettyjson — Package for formatting JSON data in a coloured YAML-style, perfect for CLI output.
- String ANSI — extends the String prototype with the function color, which inserts ANSI color codes in place of color string names.
- easy-table — Nice and simple text table for the cli
- node-prompt — Prompt the user for questions in cli!
- Shell — Nice looking shell applications with pluggable middlewares
- connect-gzip — Gzip middleware for Connect. Contains middleware for gzipping and serving static files as well as gzipping responses dynamically.
- gzip — simple compression using default linux "gzip" utility
- node-compress — streaming compression / gzip library
- node-gzbz2 — streaming compression / gzip / bzip2 library for node.js, originally forked from waveto's
- node-zlib-sync — rfc1950/rfc1951/rfc1952 compress/uncompress (C++ around libzlib)
- node-zlibstream — Streaming zlib library using zlib
- node-zip — zip/unzip support ported to Node.js from JSZip (pure JavaScript, no C++)
- node-zipfile — inspect and uncompress zipfile archives (C++ around libzip)
- zip — .zip file unpacker (pure JavaScript, no C++)
- crsh — merge and minify groups of js files and recompile when changes occur
- lzma — A standalone JavaScript implementation of the Lempel-Ziv-Markov chain (LZMA) compression algorithm
- lzw-async — Asynchronous Javascript implementation of Lempel-Ziv-Welch (LZW) compression algorithm
- node-compress-buffer — single-step Buffer compression library on top of zlib
- node-lzf - lzf compression library (C++ around liblzf)
- node-snappy — compression using Google's snappy library.
- alleup - Flexible way to resize and upload images to Amazon S3 or file system storages
- drawback — Seamless way to render 2D drawings on the client side using HTML5 technologies with a server-side backend.
- EasyImage — User-friendly module for common image editing requirements. Requires ImageMagick.
- exiv2node — Extension that provides asynchronous support for reading & writing image metadata via Exiv2.
- Face.js — Node.js module for detecting faces in images
- ffmpeg-node — Node.js driver for ffmpeg library
- ffmpeg2theora — Batch encoding with ffmpeg2theora
- gm — GraphicsMagick for node
- magician — Image manipulation for Node.js using ImageMagick.
- magickal-node — GraphicsMagick wrapper for node.js
- navcodec — Node bindings for libavcodec (ffmpeg).
- node-canvas — High performance canvas implementation backed by the phenomenal Cairo graphics library
- node-exif — Library to extract Exif metadata from images
- node-fluent-ffmpeg — Fluent API for ffmpeg (including streaming, thumbnail-generation and custom presets)
- node-gd — GD graphic library bindings
- node-gif — Convert an RGB or RGBA buffer to GIF fast
- node-graphviz — GraphViz for node
- node-handbrake — HandBrakeCLI wrapper to encode an entire folder of videos
- node-image — Unifies node-png, node-gif, node-jpeg
- node-imagemagick — ImageMagick module
- node-imager - Easy way to resize, crop and upload images to Rackspace cloudfiles
- node-jpeg — Convert an RGBA or RGB buffer to JPEG fast
- node-magick — rewrite of GraphicsMagick wrapper magickal-node fixing some issues
- node-mapnik — Mapnik bindings for high performance rendering of map tiles.
- node-mapserver — mapserver bindings for rendering mapserver MAP files via node
- node-o3-canvas — Fast HTML5 Canvas and image processing implementation based on AGG
- node-ogl — OpenGL bindings
- node-opencv — Node.js bindings for OpenCV. Originally forked from peterbraden/node-opencv
- node-pango-view — print text with pango to temporary png image, then merge it with ImageMagick.
- node-png — Convert an RGB or RGBA buffer to PNG fast
- node-qr — QR code generator for node
- node-video — Create Theora/OGG videos from RGB(A) buffers (and stream them via tag)
- node-webgl — WebGL emulation
- node-wkhtml — Convert HTML to PDF or PNG using the Webkit Rendering Engine.
- palette — Image color palette extraction library built with node-canvas
- pdfkit — A powerful PDF generation library for Node
- squeeze — Mince and convert images on the cloud using UploadJuicer .
- youtube-dl — youtube-dl driver for node
- braintree — library for integrating with Braintree
- dwolla — library for integrating with Dwolla
- node-fortumo — bindings for Fortumo SMS payment API
- node-stripe — library for integrating with Stripe
- nodewm — Webmoney signing module for NodeJs
- paynode - library for integrating with various payment gateways
- paypal-ipn — library for verifying paypal IPN messages
- ADAuthFTW - Simple Active Directory authentication (Windows Only)
- airship — an Urban Airship API wrapper
- akismet-js — A client for the Akismet spam detection API
- alleup - Flexible way to resize and upload images to Amazon S3 or file system storages
- authome - A dependency-free mutli-service authentication tool
- aws-lib — An extensible library for Amazon Web Services including EC2, SimpleDB and the Product Advertising API
- aws-snsclient — A client for handling Amazon AWS SNS endpoints
- aws2js — Amazon Web Services APIs client implementation for node.js. Simple to use and extend.
- awssum — NodeJS modules for talking to lots of Web Service APIs
- calais — Semantically analyze text using the Calais web service
- cloudfront — Amazon AWS CloudFront client
- cpanel-lib — Node.js client for the cPanel/WHM API
- discogs — Discogs API client
- dribbble-node — A wrapper for the Dribbble API
- dropbox-node — A wrapper for the Dropbox API
- ec2metadata — An API wrapper for the AWS EC2 metadata.
- evented-twitter — asynchronous twitter client, supports streaming api
- facebook-api — offers high level and low level calls against the facebook graph API
- facebook-js — Easy peasy facebook client for connect.
- fbgraph — Sexay Facebook graph api
- fitbit-js — Simple FitBit API wrapper
- flickr-reflection — A flickr client that uses their reflection API (supports auth)
- flickrnode — A library to enable use of the flickr api (not as complete as node-flickr, but supports auth)
- forrst — Simple wrapper for the Forrst.com API.
- gdata-js — Simple Google Data API OAuth 2.0 wrapper
- geo — A very basic, but simple, geocode library, currently using Google Geocode API (v3)
- gist — A gist creator. Create GISTs and use the generated URI.
- gnip — A simple library that allows you to connect to Gnip Twitter Power Track and manage rules easily.
- gravatar — Well tested Node.JS Gravatar URL generator
- gumroad — API client for Gumroad. 100% coverage of available methods.
- hipchat-js — Simple HipChat API wrapper
- hoover — An Amazon Product Advertising API wrapper
- instagram-node-lib — Library for easy interaction with the Instagram API
- janrain-api — Module for interfacing with Janrain Engage API
- klout-js — Simple Klout API wrapper
- lastfm-node — Read and write to users' recent plays on Last.fm
- libravatar — Module for generating Libravatar avatar URLs.
- linkedin-js — Easy peasy linkedin client for connect.
- madmimi-node — Client library for the MadMimi email api http://www.madmimi.com.
- mixpanel-node — A simple Mixpanel API library for server-side analytics.
- mturk — Amazon Mechanical Turk API wrapper https://github.com/expensecat/mturk.
- n-vimeo — Vimeo API (for data) Integration, retrieve info about everything (video,user,activity) with ease, an extension of vimeo-client.
- Nexmo Node API — A module for sending and receiving SMS messages via Nexmo REST API.
- node-500px — A wrapper for the http://www.500px.com API.
- node-apn — A library to send messages using the Apple Push Notification Service.
- node-armory — A simple wrapper around Blizzard's REST API for World of Warcraft.
- node-beaconpush — Client for the Beaconpush REST API. A real-time push service for browsers supporting WebSockets and Comet.
- node-bitly — A bit.ly API library for node.js - provides URL shortening/expanding. Features full API.
- node-bot — Fast and Real-time extraction of web pages information using node-dom (html,text,etc) based on given criterias
- node-c2dm — A library to send messages using the Android Cloud to Device Messaging (C2DM) service.
- node-cloudfiles — A client implementation for Rackspace CloudFiles in node.js
- node-cloudservers — A client implementation for Rackspace CloudServers in node.js
- node-dropbox — Node.js client for the Dropbox API
- node-flattr — The flattr API client.
- node-foursquare — A wrapper for the Foursquare v2 API.
- node-gadgets — Real-time extraction from web pages of HTML gadgets and associated properties using node-dom based on given criterias
- node-gdata — A generic Google Data API client library
- node-github — A wrapper for the GitHub API
- node-gitteh — Async, stable, feature-complete bindings for the libgit2 library.
- node-googleanalytics — Google Analytics GData export library
- node-googlemaps — A wrapper for the Google Maps API
- node-googleSearch — Implementation of Google Search Ajax API using node-dom
- node-gravatar — Node.js Gravatar URL generator, can be used with Node.js blogging frameworks
- node-klout — Extremely reliable Node.js Klout API Wrapper
- node-loggly --A client implementation for Loggly in node.js
- node-mailchimp — A wrapper for the MailChimp API, MailChimp Export API and MailChimp Webhooks
- node-mailgun — Client library for MailGun
- node-mixpanel-api — A simple client for the Mixpanel Data API (not event tracking! see above)
- node-neoip — binding for neoip
- node-notifo — Real-time notifications pushed to your mobile phone (and more).
- node-ocr — ABBYY API wrapper.
- node-ostatus — An implementation of the OStatus protocol (Webfinger, Push, Hcard, Salmon, etc.)
- node-pdfcrowd — A wrapper for the Pdfcrowd API. It lets you convert web pages and raw HTML code to PDF.
- node-posterous — Library for the Posterous API
- node-prowl — A module that allows you to send push notifications to your iPhone through the Prowl API
- node-rapleaf — RapLeaf API client
- node-sendgrid — Sendgrid SMTP API headers library
- node-spore — Node.js implementation of spore Generate api client with a json file.
- node-sunlightapi — A client library for the Sunlight Labs Congress API
- node-taobao — A client library for taobao.com api
- node-twilio — A helper library for the Twilio API
- node-twitter — Asynchronous Twitter REST/stream/search client for node.js
- node-untappd — Library for accessing the Untappd APIs.
- node-vk — vkontakte.ru and vk.com social network api wrapper for mobile and desktop applications.
- node-wikimapia — Node.js wrapper for the Wikimapia API
- node-yelp — A wrapper for the Yelp's APIv2. Let's you search for businesses and get specific business information.
- nodefm — So far just a util for consuming last.fm recent tracks history for a user
- nodegit — Asynchronous native Node.js libgit2 bindings with a convenient api
- nodestagram — Instagram client for node
- nodestalker — A beanstalkd client for node
- nodevore — Convore API wrapper
- openstack-storage — A module for accessing Openstack Storage (Swift)
- pingdom — A module for accessing the Pingdom JSON API
- postageapp — A module for sending emails through the PostageApp JSON API
- rackit — A simple library for managing a large number of files on Rackspace Cloud Files (CloudFiles)
- ranger — A simple library which wraps Campfire's API
- sailthru-client — A wrapper for Sailthru API
- sms — Aimed at whole SGIP/SMPP nodes support for SMS system, now focused on SP function.
- SMSified-node — A module for sending and receiving SMS messages (text messages) with the SMSified API.
- spotify — Spotify api wrapper. Metadata lookup and search. Works with Spotify and http uris
- supervisord - Library for supervisord
- tropo-webapi-node — A library for building multi-channel communication apps with the Tropo WebAPI.
- tumblr — A wrapper for Tumblr's API v2
- tumblrrr — A wrapper for Tumblr's API
- tweetstream — Stream like API for twitter's HTTP streaming interface.
- TwitScript — A port of Twython to Node.js (Twitter API Library)
- twitter-js — Easy peasy twitter client for connect.
- twitter-search — Twitter API Search w/ search result pagination, returning up to 1500 queried tweets with RegEx search, kSorts, Klout scoring and filters
- VIE — Apache Stanbol and DBpedia API client for Node.js
- vimeo-client — Vimeo API client for Node.js
- virustotal.js — VirusTotal API client for node.js
- waz-storage-js — A simple implementation of Windows Azure Storage API for Node.js
- webmetrics — A module for accessing the Webmetrics JSON API
- yammer-js — Simple Yammer API wrapper
- $N — Simple control flow with pretty syntax.
- async-mini — Minimalistic async lib implementing only .parallel() and series(). Simple code, predictable behavior, server and browser.
- async.js — Async chaining and file system utilities. Async.js is to node's fs module, what jQuery is to the DOM.
- async — Comprehensive async map/reduce and control flow (parallel, series, waterfall, auto...) module that works in node and the browser
- asyncblock — Simple and powerful interface to fibers
- atbar — Async callback manager for javascript in nodejs and browser
- backbone-callbacks — Node.js style callbacks for Backbone.js asynchronous methods
- begin — Control flow library for node.js and CoffeeScript
- chainsaw — Build chainable fluent interfaces the easy way in node.js
- channels — Event channels for node.js
- Cinch — Write async code in sync form.
- cloudd — Job manager, runs set of tasks defined using a DAG definition
- deferred — Asynchronous control-flow with deferred and promises
- each — Chained and parallel async iterator in one elegant function
- EventProxy.js — An implementation of task/event based asynchronous pattern. Use events to avoid dirty callbacks.
- fiberize — Node API wrapper for use with fibers.
- fibers-promise — Small yet powerful promises based on fibers.
- fibers — The closest thing to a thread you'll find in JavaScript
- first — A tiny control-flow library.
- flow-js — Continuation-esque contruct for expressing multi-step asynchronous logic
- flowless — Less but better control-flow library.
- funk — Asynchronous parallel functions made funky!
- futures - Asynchronous Method Queueing, Futures, Promises, Subscriptions, and other async goodies
- gate — An utility to await multiple asynchronous calls
- groupie — A simple control flow library for executing multiple functions as a group or in a chain, calling back when complete.
- Ignite — An easy-to-use async programming framework inspired by UML2 state machines.
- jam — Monadic continuation/callbacks/async helper.
- JobManager — A really simple tool that helps you manage multiple asynchronous tasks.
- jscex-jquery — Jscex adapter to jQuery.Deferred API, with $.async/$await support
- Jscex — Transforms sync code into monadic, async code as the async support in F# and Scala.
- LAEH2 — Lightweight Asynchronous Error Handling v2. Wrap your asynchronous functions in an intelligent try/catch. Optional stack trace minification with async support, e.g.: unexpected thing < ./ex1.js(9) << ./ex1.js(7 < 13).
- locker (server) and node-locker (client) — lock server and client for distributed systems with waiting and execution timeouts.
- miniqueue — A very simple in-memory queue for easy sequential processing
- narrow — Shrinks a given callback parallel execution concurrency in a limited number of threads, receiving a big bunch of data (array of tasks)
- nestableflow — Asynchronous nestable flow-control (serial, parallel, repeat, wait, function) module for Node.js, RequireJS, and browser.
- neuron — The simplest possible event driven job manager, FIFO queue, and "task based cache" in node.js
- node_memo — Sophisticated function memoization
- node-async — Executes a number of functions and returns the results of each function, when all are complete, for processing. Based on Narrow.
- node-block — An async control-flow library. Easily error handling.
- node-chain — Simple call chaining library for node.js
- node-continuables — A library making dealing with asynchronous code easier
- node-cron — Schedule callbacks with cron syntax
- node-crontab — Allows reading, manipulating, and writing user crontabs from node.js
- node-fnqueue — Functions chain utility
- node-inflow — A next generation async control-flow library, with a shared object for called functions and debugging made easy.
- node-parallel — Create tasks in node.js that run in parallel, or sequences that run tasks one after another.
- node-promise — Robust promises for node.js, includes promise utilities and promise-based I/O library
- node.flow — A deadly simple flow control package for node.js
- noflo — Flow-Based Programming system for Node.js
- nue — An async control-flow library suited for node.js
- observer — An alternative observer implementation, for both ss&cs.
- pauseable — Easily pause and resume event emitters, timeouts, and intervals.
- pipette — A collection of pipe / stream utility classes.
- poolr — A lightweight resource pooling/serialization library
- promise — A Continuation handler (monad) with a condition system for errors
- q — Q is a tool for making and composing asynchronous promises in JavaScript.
- QBox — Quick Controller flow for NodeJS
- read-files — Asynchronously read a bunch of files and give a callback when reading is complete.
- Rubberduck — Punch JavaScript objects and receive events before and after a function executes.
- SCION — An implementation of SCXML/Statecharts in JavaScript.
- seq — Chainable asynchronous control flow for node.js with sequential and parallel primitives and pipeline-style error handling
- sexy — Proxy (ES5) for writing sequential asynchronous code in a synchronous style
- Signals — Custom Event/Messaging system which supports advanced features, doesn't rely on strings for the event publishing/subscribing and favor composition over inheritance.
- simple-schedule — Simple scheduler for dispatching a number of batch tasks at defined intervals
- sjs — Asynchronous JavaScript for synchronous people
- Slide — A control flow library that fits in a slide presentation
- soda.js — Asynchonous JavaScript module loader for client-side and Node.js
- Step — Tool for grouping and chaining asynchronous callbacks, based on flow-js
- stepc — A CoffeeScript-safe version of creationix' step
- streamline.js — Tool that transforms synchronous code into asynchronous code with callbacks.
- sync — Write simple and readable synchronous code in nodejs using fibers (based on node-fibers)
- taskman — A task management library for managing (possibly) asynchronous tasks by declaring task dependencies.
- timequeue — A queue with custom concurrency and time limit.
- TwoStep — An async control-flow library that makes stepping through logic easy. The spiritual successor of Step with better error handling and finer flow control.
- when — A lightweight CommonJS Promises/A and when() implementation. It also provides several other useful Promise-related concepts, such as joining and chaining, and has a robust unit test suite.
- zo.js — Async-friendly versions of the usual list processing functions: map, select, reduce and friends.
- cld — A straight port of the CLD (Compact Language Detector) library embedded in Google's Chromium browser.
- connect-i18n — Middleware for connect to handle i18n.
- dialect-http — http client to manage your dialect translations.
- dialect — Translations for nodejs.
- i18n-node — Exports common __(), __n() syntax. Stores JSON objects dynamically on code-change.
- i18next — express middleware, template support, clientside support
- inflect — A port of the Rails / ActiveSupport inflector to JavaScript (node.js and browser compatible).
- Lingo — Inflection, transformation and more
- lingua — A i18n middleware for the Express.js framework.
- localize — A localization module with Date support and simple Express integration.
- node-date-diff — Calculates date differences and allows to format it nicely to your language (Eg. In one hour)
- node-gettext — Use Gettext .MO files with Node.js.
- node-i18n — node-i18n is a minimalistic internationalization module with templating
- node-language-detect — NodeJS language detection library using n-gram (which can identify up to 52 human languages)
- translate.js — translate text from one language to another on node.js and the browser. 30+ languages supported, simple as cake.
- unidecode — ASCII transliterations of Unicode text
- Alice — Killer Node.js + Mocha Boilerplate for creating NPM packages for both Javascript and Coffee-Script coders.
- infigo-node-boilerplate — nodejs boilerplate built upon Express, db-mysql/db-drizzle/Mongoose and includes user authentication and group+role authorization system.
- Lemmy — 49% Motherfucker | 51% Son of a Bitch Node.js + Express + Mocha flexible project boilerplate for Javascript/Coffee-Script coders.
- node-backbone — Sample project for a Node/Backbone/HTML5 application optimized separately for desktop browsers (web), generic mobile platforms using jQuery Mobile (jquerymobile) and iPhone (iphone). Uses Express, Socket.io, Mongoose/MongoDB, Redis.io (for sessions) and Mocha (for testing).
- node-boilerplate — Everything you need to get started on a Node.js website with express and socket.IO
- NodeBase — A node base class for JavaScript and CoffeeScript (logging, options, defaults and EventEmitter)
- npm-boilerplate — Boilerplate for creating NPM packages
- Skeleton — Skeleton is a CLI module to bootstrap a Node/Express project with a skeleton template via any git repository, with an included default repo.
- transsiberian — [outdated] nodejs boilerplate built upon Express and Mongolia
- Concrete — Simple continuous integration server written with NodeJS and CoffeeScript
- Jenkins API — An API written in Node for Jenkins, allowing you to trigger jobs, delete jobs, copy jobs, etc.
- nestor — Jenkins Node.js CLI
- cloudjs — provides a network distributed event system and a
- node-cqrs-domain — Node-cqrs-domain is a node.js module based on nodeEventStore that. It can be very useful as domain component if you work with (d)ddd, cqrs, eventdenormalizer, host, etc.
- node-cqrs-eventdenormalizer — Node-cqrs-eventdenormalizer is a node.js module that implements the cqrs pattern. It can be very useful as eventdenormalizer component if you work with (d)ddd, cqrs, domain, host, etc.
- node-cqrs — node.js cqrs implementation. CouchDb repository embedded, others can be plugged in. Contains basic implementation of denormalized view builder with storing data snapshots.
- node-cqs — Node-cqs is a node.js module that implements the cqrs pattern without eventsourcing. It can be very useful as domain and eventdenormalizer component if you work with (d)ddd, cqrs, host, etc.
- node-queue — Node-queue is a node.js module for multiple databases. It can be very useful if you work with (d)ddd, cqrs, eventsourcing, commands and events, etc.
- node-viewmodel — Node-viewmodel is a node.js module for multiple databases. It can be very useful if you work with (d)ddd, cqrs, eventdenormalizer, host, etc.
- nodeEventedCommand — provides simple command/event handling for evented systems like cqrs
- nodeEventStore — EventStore (Events and Snapshots) supporting MongoDb, CouchDb, Redis
- rule-validator — Rule-validator is a javascript module based on amanda. And works for node.js and in the browser. It can be very useful if you work with (d)ddd, cqrs, eventsourcing, domain, commands and events, etc. realtime balancing for live objects
- appjs — SDK on top of nodejs to build desktop apps with html/css/js.
- clipboard — Easy to use utility for reading and writing to the system clipboard.
- Threads A GoGo — Run cpu-bound JavaScript tasks non-blockingly in background kernel threads. With EventEmitter and continuation passing style APIs.
- Nervous — Pluggable monitoring system with support for Graphite.
- Response — Pluggable alerting system and Graphite proxy.
- zabbix-agent-client — getting data from zabbix_agent servers.
- append — Append properties from one object to another.
- attache.js - Very basic AOP implementation
- audience-meter — A simple audience meter based on Socket.IO, perfect for live events tracking
- behaviors — A simple way to check a module's exports, useful for 3rd party plugins and extensions
- box2dnode — Port of the box2d physics simulation library
- browserify — Browser-side require() for your node modules and npm packages
- bullet — Bullet Physics for node.js
- crud-bones — CRUD boilerplate/template for Node.JS/Mongo/MySQL/Redis/Express/EJS/Cluster/etc.
- descriptive-statistics — Descriptive Statistics for JavaScript
- difflib.js — Text diff library, ported from Python's difflib module
- Gauss - JavaScript statistics and analytics library
- GFMS — Github Flavored Markdown Server. Preview your README.md (and other markdown docs) locally before committing it. Uses Github's CSS for faithful representation. Uses socket.io to automatically reload your browser upon file changes.
- highlight.js — Syntax Highlighting (for node.js and browser)
- Jalali — Gregorian to Jalali Converter
- jsbundle — Simple, clean, and automatic bundling of your Node modules and packages for use in the browser.
- model.js — Simple data validation for both server and browser.
- node-ast-inlining — A small library that inline and expand function call
- node-dummy-cache — A simple in memory cache to use with nodejs.
- node-stem — Bindings to the libstemmer library
- node-talib — Technical Analysis Library with 100+ indicators and candlestick pattern recognition
- node-upload-progress — It's a Node.js module to handle upload and upload-progress
- node.packer — An assets combine and minify tool
- nova — Node.js libraries ported to the browser. Write once, run everywhere.
- numpad — Pad numbers with leading zeros to any number of digits
- oid — Utilities for object identity
- randexp.js - Create random strings that match a given regular expression.
- short — NodeJS URL Shortener backed by MongooseJS w/ Complete Example Site
- sigar — Node Binding to SIGAR (System Information Gatherer And Reporter)
- sorter — Sort methods: dictionary sort and natural sort
- toSrc — Turns every JavaScript object or primitive into valid source code that can be evaled again.
- tty.js - a terminal for your browser
- version — NodeJS package.json version number fetcher
- yui — YUI available in Node.js, (extra docs)
There are plenty of CouchDB tutorials and the CouchDB definitive guide is available online, but having CouchDB basics — setup, admin interface, user management, common access patterns — presented in a single page can still be useful. Gavin Cooper has published such a short guide to CouchDB:
NoSQL has been one of the most talked about topics over the past couple of months. This tutorial will introduce you to CouchDB, a NoSQL implementation and teach you how to get started with the platform.
- Difficulty: Easy
- Estimated Completion Time: 45 Minutes
Original title and link: CouchDB Tutorial: Getting Started with CouchDB (NoSQL databases © myNoSQL)
via: http://net.tutsplus.com/tutorials/getting-started-with-couchdb/
Looking for a good tutorial on Express.js to help you get quickly productive in it? You have come to the right place.
In this tutorial I will run you through the process setting up an Express.js app and making it do what a basic website might do. You will learn the basics of routes, views, Jade templates, Stylus CSS engine, handling POST and GET requests.
Let's create an online store to sell Ninja items. We will call it the Ninja Store.
Installing Express
First of all install Express.js as a global module:
$ npm install express -gIf that fails:
$ sudo npm install express -gNow, create a directory and create an Express app in it:
$ mkdir ninja-store
$ cd ninja-store
$ express --sessions --css stylusIf you like shortcuts, you could accomplish the same this way too:
$ express ninjia-store --sessions --css stylus && cd ninja-store
We want to have support for sessions, hence the --sessions option. Using --css, we specified that we would be using the Stylus CSS engine. If you are a Less fan, you can specify --css less. Not specifying the --css option will default to the plain vanilla CSS we all are familiar with.Then install the dependencies for the app:
$ npm installThat will install a bunch of modules used by the app. With that the base of the app is ready. It is already a working app. Let's see what it outputs. Start the app:
$ node app
Express server listening on port 3000 in development modeThen load http://localhost:3000/ in your browser. You will see a simple webpage with the title "Express", and the webpage says:
Express
Welcome to ExpressSo looks like the app is working. Time to find out how it works.
Request flow in Express
This is how a request to an Express server flows:
Route → Route Handler → Template → HTML
The route defines the URL schema. It captures the matching request and passed on control to the corresponding route handler. The route handler processes the request and passes the control to a template. The template constructs the HTML for the response and sends it to the browser.
The route handler need not always pass the control to a template, it can optionally send the response to a request directly .
To understand how Express works, let's get working on the Ninja Store app.
Setting up the Routes
We will modify some of the stuff Express generated for us to make it more suited for our app, and more logical so that you can understand the inner workings of Express better.
Rename the index.jade file in the views folder to home.jade. This is actually a part of setting up views in Express.js, I will get there in the next section.
Delete the index.js file in the routes folder. We don't need it and its presence could be potentially confusing. Create a new file called store.js in the routes folder, and put the following code in it:
exports.home = function(req, res){
res.render('home', { title: 'Ninja Store' })
};Then we are going to modify app.js:
Change this
var express = require('express')
, routes = require('./routes');to
var express = require('express');
var store = require('./routes/store');and this
app.get('/', routes.index);to
app.get('/', store.home);With that we have set up our own route for the home page.
Routes are URL schema for a website. In Express you define them using app.get(), app.post(), app.delete() etc. The get, post, delete methods are derived from their corresponding HTTP verbs.
So far we created a single route for our app. Very soon we will be creating more, but before that I'll tell you about the files in the routes directory.
The routes directory is a convention, not a compulsion. In the routes directory we create appropriately named files which will handle the routes we define in the app.js file. We will import these files into our app and assign the functions defined in them to handle various routes.
The imported file becomes sort of like an instance of the class of the route handler file. In our case ./routes/store.js is the class, store is instance, and store.home is a method of the instance.
Again as a recommended convention, we create an appropriately named variable for the imported file from the routes directory. Then we pass one of its functions as the second parameter for the route. For eg:
app.get('/', store.home);From that you might probably guess, we can pass any function as the second parameter for the route. You are correct, even this would work:
app.get('/', function(req, res) {
res.render('home', { title: 'Ninja Store' });
});We don't need to render HTML pages either:
app.get('/', function(req, res) {
res.send('Look ma, no HTML!');
});Now run the app again and see what you get. Title has changed to "Ninja Store" and you see:
Ninja Store
Welcome to Ninja StoreWe are making progress. Time to spice up the home page
Rendering Views
You have seen res.render() and res.send() in action already and probably have a fair idea about what they do. res.send() will directly send a string of text to the browser and res.render() will render a Jade template.
While it is possible to create a website entirely using res.send(), it is certainly not recommended to do so, because you will only end up with a complex and dirty looking codebase. Jade is the default templating engine for Express. Let's find out the basics of res.render() and the Jade templates.
Open the file named layout.jade in the views directory. Let's examine its content:
!!!
html
head
title= title
link(rel='stylesheet', href='/stylesheets/style.css')
body!= bodyThat's Jade code. The very basics of Jade is this:
i. HTML tag names to create tags, but without the angular brackets
ii. Spaces or tabs for indentation, but don't mix them
iii. # to assign an id to a tag
iv. . to assign a class to a tag, or create a div with a class if not already created
v. (property='value') to create attributes and assign values to a tagJade is out of scope of this tutorial, so you might want to read more about it here. However, as we proceed with the tutorial, I will explain you the relevant Jade code we come across.
The !!! you see in layout.jade is doing a doctype declaration of HTML5. Also notice the relatively 'cryptic' title= title and body!= body. This is what they are doing:
The following is an explanation of two pieces of code that might intrigued you.
title= title: The template expects a variable called title from the route handler. The variable will be set as the content of the title tag AFTER escaping special characters.
body!= body: The template expects a variable called body from the route handler which called the red.render() method. The variable will be set as the content of the body tag WITHOUT escaping special characters. If special characters like < and > are escaped, we won't have any HTML code within the body tag, hence we don't want the contents of the body variable to be escaped.
Our view rendering code is res.render('home', { title: 'Ninja Store' }), so where does layout.jade come into the picture? Let me explain you how the rendering works.
Our renderer code looks for a Jade template named home.jade in the views folder and passes an object to it. In our case it is { title: 'Ninja Store' }. The properties of the object will be available as variable names in the template with their respective name. In our case, we will have a variable named title in home.jade.
Invisible to the naked eye, another property is 'sent' to the template - layout with a default value of layout.jade. So the 'actual' renderer code can be understood as res.render('home', { layout: 'layout', title: 'Ninja Store' }).
When a template gets the layout variable. It makes all its variables available to the layout with their respective names, and itself in a variable named body. Now do you see how
title= titleand
body!= bodywork?
Since layout has a default value, probably it might make you wonder if you could specify a layout of your own choice, or maybe even not use one. Your guesses are right.
To specify a custom layout do this:
{ layout: 'layout-ie', title: 'Ninja Store' }If you don't want to use a layout do this:
{ layout: false, title: 'Ninja Store' }Try them.
By default res.send() and res.render() send the HTTP status code of 200. You can specify your own HTTP status code.
Custom status code example for res.send():
res.send('File not Found', 404);Custom status code example for res.render() (make sure you have created a file named 404.jade in the views directory):
res.render('404', { title: 'File not Found', status:404 });Ok, now that we know the basic of views, let's revamp our homepage!
Put the following code in the home.jade file:
#container
#logo
img(src='/images/logo.png')
#display
#login
form(method='post')
| Enter your name if you want to be a ninja
div
input(type='text', name='username')
input(type='submit', value='Log In')
#footer
div Copyright © Ninja Store #{+new Date().getFullYear()}
a(href='/page?name=about') About
| |
a(href='/page?name=contact') Contact UsMake sure you create a nice logo for the store, name it logo.png, and keep it in the /public/images/ directory.
Put the following code in the style.styl file in the /public/stylesheets/ directory:
body
padding: 50px
font: 14px "Lucida Grande", Helvetica, Arial, sans-serif
background: #ccca
color: #0069FF#container
width: 450px
margin: 0 auto
padding: 40px 20px
background: #fff
box-shadow: 1px 3px 3px #333
border-radius: 5px#logo
text-align: center#display
margin: 20px 0 50px#userbar
margin-bottom: 10pxAren't we supposed to edit the style.css file? Well, style.styl is the Stylus file which generates the style.css file. Even though we code our CSS in the .styl file, we always include the .css file in the Jade template:
link(rel='stylesheet', href='/stylesheets/style.css')So what is the point of using Stylus? Stylus offers a dynamic and efficient syntax for generating CSS. The details is out of scope of this tutorial, you can learn more about Stylus here.
Note: just like Jade templates, make sure you either uses spaces or tabs consistently for indenting your Stylus code.
Changes made to the views and everything in the public directory will be updated immediately when you refresh the browser. Refresh the browser and see what you get. In rare cases, the view may not be update - feel free to restart the server.
Wow! There you have, a log in form. Try logging in.
Meh! Hit by an error:
Cannot POST /Let me explain what's going on.
Remember the route
app.get('/', store.home);
we created?That was for GET requests to the root of the website. Since our form uses the POST method, the route is not capturing it. We need to create a POST router for the home page to process the form.
Handling Forms - POST, GET Requests
Add this to app.js:
app.post('/', store.home_post_handler);Now we need to define the home_post_handler() function in store.js module. Add the following to store.js:
// handler for form submitted from homepage
exports.home_post_handler = function(req, res) {
// if the username is not submitted, give it a default of "Anonymous"
username = req.body.username || 'Anonymous';
// store the username as a session variable
req.session.username = username;
// redirect the user to homepage
res.redirect('/');
};So handling POST data is pretty easy - just look for them in the req.body object.
Refresh the browser.
You still get Cannot POST /. That's because for changes you make in .js files to take effect, you need to restart the server. So restart the server and try logging in again.
This time the form submission works fine, but you still see the log in form. Did we log in or not? We did log in, but our route handler made it confusing. We'll change it now. Edit the exports.home() function:
// handler for homepage
exports.home = function(req, res) {
// if user is not logged in, ask them to login
if (typeof req.session.username == 'undefined') res.render('home', { title: 'Ninja Store'});
// if user is logged in already, take them straight to the items list
else res.redirect('/items');
};Restart the server and log in again.
Cannot GET /itemsAn error again. But it looks more promising this time, the app is trying to load an undefined route. If we define the route, we would have it working.
Before we get to defining the missing route, let's optimize the views a little bit. Some components of the view will be common to all the views, so it is a good idea to modularize them. The Jade template engine has a command called include using which you can include files into a view.
Create a file called footer.jade in the views directory with the following content:
#footer
div Copyright © Ninja Store #{+new Date().getFullYear()}
a(href='/page?name=about') About
| |
a(href='/page?name=contact') Contact UsCreate a file called userbar.jade in the views directory with the following content:
#userbar
| Welcome #{username} |
a(href='/items') Items
| |
a(href='/logout') Log OutNow edit the home.jade file:
#container
#logo
a(href='/')
img(src='/images/logo.png')
#display
#login
form(method='post')
| Enter your name if you want to be a ninja
div
input(type='text', name='username')
input(type='submit', value='Log In')
include footerWe will be defining the missing items/ and a related route in a while. In the process, we will also find out how to handle GET requests in Express.js.
There are two types of parametric GET requests in Express.js - req.params and req.query.
The req.params object contains request parameters right in the URL. It uses the clean URL scheme. Eg: http://example.com/product/1274.
The req.query object contains request parameters in the GET query. It uses the traditional GET request scheme. Eg: http://example.com?product=1274.
For displaying the items we will use the req.params method.
Add these routes to app.js:
// display the list of item
app.get('/items', store.items);
// show individual item
app.get('/item/:id', store.item);/items for listing the items of the store.
/item/:name for displaying the individual items.
We will use a simple hard-coded array as the data source. Add the following to the store.js file.
// our 'database'
var items = {
SKN:{name:'Shuriken', price:100},
ASK:{name:'Ashiko', price:690},
CGI:{name:'Chigiriki', price:250},
NGT:{name:'Naginata', price:900},
KTN:{name:'Katana', price:1000}
};Now create the corresponding route handlers for the routes we added. Add the following to store.js:
// handler for displaying the items
exports.items = function(req, res) {
// don't let nameless people view the items, redirect them back to the homepage
if (typeof req.session.username == 'undefined') res.redirect('/');
else res.render('items', { title: 'Ninja Store - Items', username: req.session.username, items:items });
};// handler for displaying individual items
exports.item = function(req, res) {
// don't let nameless people view the items, redirect them back to the homepage
if (typeof req.session.username == 'undefined') res.redirect('/');
else {
var name = items[req.params.id].name;
var price = items[req.params.id].price;
res.render('item', { title: 'Ninja Store - ' + name, username: req.session.username, name:name, price:price });
}
};Time to create the views for the routes.
View for listing all the items on the store. Name it items.jade.
#container
#logo
img(src='/images/logo.png')
#display
include userbar
-for (var id in items)
- var item = items[id]
div
a(href='/item/#{id}') #{item.name} - $#{item.price}
include footerView for listing individual items of the store. Name it item.jade.
#container
#logo
img(src='/images/logo.png')
#display
include userbar
p The #{name.toLowerCase()} is one of the must-have items for any aspiring ninja. It costs just $#{price} on our store.
p Buy it today!
include footerOur footer links uses the traditional GET style of links, let's find out how to handle those kind of requests.
First create the routes for the footer links. Add the following to app.js.
// show general pages
app.get('/page', store.page);Now, the route handler in store.js:
// handler for showing simple pages
exports.page = function(req, res) {
var name = req.query.name;
var contents = {
about: 'Ninja Store sells the coolest ninja stuff in the world. Anyone shopping here is cool.',
contact: 'You can contact us at <address><strong>Ninja Store</strong>,<br>1, World Ninja Headquarters,<br>Ninja Avenue,<br>NIN80B7-JP,<br>Nihongo.</address>'
};
res.render('page', { title: 'Ninja Store - ' + name, username: req.session.username, content:contents[name] });
};And then the view for the pages. Create a new view file named page.jade in the views directory with the following content.
#container
#logo
a(href='/')
img(src='/images/logo.png')
#display
p!= content
include footerNotice how we use p!= content instead of p #{content}. That is because we don't want the Jade engine to escape the HTML content of the variable.
we need to create a route and a handler for logging out. Add this route to app.js:
app.get('/logout', function(req, res) {
// delete the session variable
delete req.session.username;
// redirect user to homepage
res.redirect('/');
});Notice how we specified the route handler right in the app.js file with the route definition. All the route definition cares about is that the second parameter should be a function, wherever it may be defined; just make sure to pass the request object (req) and the response object (res) to it.
With that our Ninja Store website ready. Start the app and get ready to witness the brilliance of the Ninja Store in action.
Conclusion
You know what? The whole project is on GitHub
I intentionally didn't tell you earlier, so that you don't get lazy and skip learning. Now that you done the hard work, feel free to clone the Ninja Store repository and go berserk on it.
Express.js is a lot more than what I covered in this tutorial. I have already, and will be covering the more advanced topics in dedicated articles on my website.
Subscribe to my RSS feed and follow me on Twitter for excellent articles on Node.js and Express.js.
PS: I have a small exercise for you. The view can be further modularized, how will you do that?
Socket.IOSocket.IO is a Node.JS project that makes WebSockets and realtime possible in all browsers. It also enhances WebSockets by providing built-in multiplexing, horizontal scalability, automatic JSON encoding/decoding, and more.
How to Install
How to use
First, require socket.io:
var io = require('socket.io');Next, attach it to a HTTP/HTTPS server. If you're using the fantastic express web framework:
var app = express.createServer() , io = io.listen(app); app.listen(80); io.sockets.on('connection', function (socket) { socket.emit('news', { hello: 'world' }); socket.on('my other event', function (data) { console.log(data); }); });Finally, load it from the client side code:
<script src="/socket.io/socket.io.js"></script> <script> var socket = io.connect('http://localhost'); socket.on('news', function (data) { console.log(data); socket.emit('my other event', { my: 'data' }); }); </script>For more thorough examples, look at the examples/ directory.
Short recipes
Sending and receiving events.
Socket.IO allows you to emit and receive custom events. Besides connect, message and disconnect, you can emit custom events:
// note, io.listen(<port>) will create a http server for you var io = require('socket.io').listen(80); io.sockets.on('connection', function (socket) { io.sockets.emit('this', { will: 'be received by everyone' }); socket.on('private message', function (from, msg) { console.log('I received a private message by ', from, ' saying ', msg); }); socket.on('disconnect', function () { io.sockets.emit('user disconnected'); }); });Storing data associated to a client
Sometimes it's necessary to store data associated with a client that's necessary for the duration of the session.
Server side
var io = require('socket.io').listen(80); io.sockets.on('connection', function (socket) { socket.on('set nickname', function (name) { socket.set('nickname', name, function () { socket.emit('ready'); }); }); socket.on('msg', function () { socket.get('nickname', function (err, name) { console.log('Chat message by ', name); }); }); });Client side
<script> var socket = io.connect('http://localhost'); socket.on('connect', function () { socket.emit('set nickname', prompt('What is your nickname?')); socket.on('ready', function () { console.log('Connected !'); socket.emit('msg', prompt('What is your message?')); }); }); </script>Restricting yourself to a namespace
If you have control over all the messages and events emitted for a particular application, using the default / namespace works.
If you want to leverage 3rd-party code, or produce code to share with others, socket.io provides a way of namespacing a socket.
This has the benefit of multiplexing a single connection. Instead of socket.io using two WebSocket connections, it'll use one.
The following example defines a socket that listens on '/chat' and one for '/news':
Server side
var io = require('socket.io').listen(80); var chat = io .of('/chat') .on('connection', function (socket) { socket.emit('a message', { that: 'only', '/chat': 'will get' }); chat.emit('a message', { everyone: 'in', '/chat': 'will get' }); }); var news = io .of('/news'); .on('connection', function (socket) { socket.emit('item', { news: 'item' }); });Client side:
<script> var chat = io.connect('http://localhost/chat') , news = io.connect('http://localhost/news'); chat.on('connect', function () { chat.emit('hi!'); }); news.on('news', function () { news.emit('woot'); }); </script>Sending volatile messages.
Sometimes certain messages can be dropped. Let's say you have an app that shows realtime tweets for the keyword bieber.
If a certain client is not ready to receive messages (because of network slowness or other issues, or because he's connected through long polling and is in the middle of a request-response cycle), if he doesn't receive ALL the tweets related to bieber your application won't suffer.
In that case, you might want to send those messages as volatile messages.
Server side
var io = require('socket.io').listen(80); io.sockets.on('connection', function (socket) { var tweets = setInterval(function () { getBieberTweet(function (tweet) { socket.volatile.emit('bieber tweet', tweet); }); }, 100); socket.on('disconnect', function () { clearInterval(tweets); }); });Client side
In the client side, messages are received the same way whether they're volatile or not.
Getting acknowledgements
Sometimes, you might want to get a callback when the client confirmed the message reception.
To do this, simply pass a function as the last parameter of .send or .emit. What's more, when you use .emit, the acknowledgement is done by you, which means you can also pass data along:
Server side
var io = require('socket.io').listen(80); io.sockets.on('connection', function (socket) { socket.on('ferret', function (name, fn) { fn('woot'); }); });Client side
<script> var socket = io.connect(); // TIP: .connect with no args does auto-discovery socket.on('connect', function () { // TIP: you can avoid listening on `connect` and listen on events directly too! socket.emit('ferret', 'tobi', function (data) { console.log(data); // data will be 'woot' }); }); </script>Broadcasting messages
To broadcast, simply add a broadcast flag to emit and send method calls. Broadcasting means sending a message to everyone else except for the socket that starts it.
Server side
var io = require('socket.io').listen(80); io.sockets.on('connection', function (socket) { socket.broadcast.emit('user connected'); socket.broadcast.json.send({ a: 'message' }); });Rooms
Sometimes you want to put certain sockets in the same room, so that it's easy to broadcast to all of them together.
Think of this as built-in channels for sockets. Sockets join and leave rooms in each socket.
Server side
var io = require('socket.io').listen(80); io.sockets.on('connection', function (socket) { socket.join('justin bieber fans'); socket.broadcast.to('justin bieber fans').emit('new fan'); io.sockets.in('rammstein fans').emit('new non-fan'); });Using it just as a cross-browser WebSocket
If you just want the WebSocket semantics, you can do that too. Simply leverage send and listen on the message event:
Server side
var io = require('socket.io').listen(80); io.sockets.on('connection', function (socket) { socket.on('message', function () { }); socket.on('disconnect', function () { }); });Client side
<script> var socket = io.connect('http://localhost/'); socket.on('connect', function () { socket.send('hi'); socket.on('message', function (msg) { // my msg }); }); </script>Changing configuration
Configuration in socket.io is TJ-style:
Server side
var io = require('socket.io').listen(80); io.configure(function () { io.set('transports', ['websocket', 'flashsocket', 'xhr-polling']); }); io.configure('development', function () { io.set('transports', ['websocket', 'xhr-polling']); io.enable('log'); });License
(The MIT License)
Copyright (c) 2011 Guillermo Rauch <guillermo@learnboost.com>
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the 'Software'), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.