restify-magic

Magic resources for restify

Restify Magic

Magic resources for restify

npm install restify-magic

The first example illustrates the basic usage of restify-magic. It initializes a restify server that maps arbitrary REST resources to /:resource with the resources stored in a local NeDB database:

require("restify-magic")
  .createServer()
  .listen(3000);

The second example represents a good starting point for a more complex application. It initializes a restify server that maps arbitrary REST resources to /r/:resource and shares the contents of the public/ directory under /:

var restify = require("restify"),
  magic = require("restify-magic"),
  server;

// create the rest server
server = magic.createServer({ prefix: "r", debug: process.env.DEBUG });

// static server
server.get("/.*", restify.serveStatic({
  directory: "./" + (process.env.PUBLIC || "public"),
  default: "index.html"
}));

// start listening
server.listen(process.env.PORT || 3000, function () {
  console.log("Server listening at %s", server.url);
});

Note that above example allows you to toggle detailed logging via the environment variable DEBUG, as well as specifying a custom public directory or listen port via the PUBLIC and PORT variables.

createServer accepts the following configuration options:

  • store: [restify.Store]: A custom data store to be used
  • prefix: [string]: URL prefix to be prepended before all resource URLs
  • server: [restify.Server]: Custom restify server to use
  • debug: [bool]: Enable debug logging (all requests are logged to the console)
  • middleware: [Array]: An array of custom middleware to be added to the restify server

If the body of a post or put request contains a key called password, a salt sha-1 hash is generated and used to store the given password in encrypted form. Likewise, if a get request contains the key password in its parameters, it the password is matched with the stored encrypted password.

Usage example:

var restify = require("restify"),
  magic = require("restify-magic"),
  store = magic.nedbStore({ path: "path/to/your/db" }),
  server = magic.createServer({ store: store });

By providing a path option, a separate nedb datafile is created in the path directory for each resource.

MIT