node package manager


Restify Magic

Magic resources for restify


npm install restify-magic

Usage Example

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:


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"),

// 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.

Configuration Options

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.

Custom Data Stores


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.