nl_tokens: the rest server
pl_tokens: function createServer ( options ) { var server , stores ; // convenience method to define a restify route function defineRoute ( method , route , handler ) { server [ method ] ( route , function ( req , res ) { var store = stores . get ( req . params . resource ) ; delete req . params . resource ; handler ( req , store , function ( error , docs ) { if ( error ) { res . status ( 500 ) ; res . end ( error ) ; } else if ( ! docs ) { res . status ( 404 ) ; res . end ( "Not found" ) ; } else { res . json ( docs ) ; } } ) ; } ) ; } // convenience method to create a routing URL function routeURL ( ) { var prefix = options . prefix || "" , args = [ "/" + prefix , ":resource" ] . concat ( Array . prototype . slice . call ( arguments ) ) ; return path . join . apply ( path , args ) ; } // create password salt and hash function createPasswordHash ( object ) { if ( ! object ) return object ; var password = object . password ; if ( password ) { delete <mask> . password ; object . passwordSalt = sha1 ( Math . random ( ) . toString ( ) ) ; object . passwordHash = sha1 ( "restify-magic" + object . passwordSalt + password ) ; } return object ; } // ensure non-empty options options = options || { } ; // the store manager options . store = options . store || defaultStore ( ) ; stores = new StoreManager ( options ) ; // create and configure the server server = options . server || restify . createServer ( ) ; server . use ( restify . plugins . acceptParser ( server . acceptable ) ) . use ( restify . plugins . fullResponse ( ) ) . use ( restify . plugins . queryParser ( ) ) . use ( restify . plugins . bodyParser ( { mapParams : false } ) ) . use ( typeConvertMiddleware ) . use ( restify . plugins . gzipResponse ( ) ) . pre ( restify . pre . sanitizePath ( ) ) ; if ( options . debug ) { server . use ( logger ) ; } if ( options . middleware ) { options . middleware . forEach ( function ( middleware ) { server . use ( middleware ) ; } ) ; } // GET defineRoute ( "get" , routeURL ( ) , function ( req , store , cb ) { var password = req . params . password ; if ( ! password ) { return store . find ( req . params ) . sort ( { _created : 1 } ) . exec ( cb ) ; } // password to hash delete req . params . password ; store . findOne ( req . params ) . exec ( function ( err , record ) { if ( err ) { cb ( err ) ; } else if ( sha1 ( "restify-magic" + record . passwordSalt + password ) !== record . passwordHash ) { cb ( undefined , [ ] ) ; } else { cb ( undefined , record ) ; } } ) ; } ) ; // GET with id defineRoute ( "get" , routeURL ( ":_id" ) , function ( req , store , cb ) { store . findOne ( req . params , cb ) ; } ) ; // POST defineRoute ( "post" , routeURL ( ) , function ( req , store , cb ) { createPasswordHash ( req . body ) ; req . body . _updated = new Date ( ) ; if ( Object . keys ( req . params ) . length > 0 ) { store . update ( req . params , req . body , { multi : true , upsert : true } , cb ) ; } else { req . body . _created = req . body . _updated ; store . insert ( req . body , cb ) ; } } ) ; // POST with id defineRoute ( "post" , routeURL ( ":_id" ) , function ( req , store , cb ) { createPasswordHash ( req . body ) ; req . body . _updated = new Date ( ) ; store . update ( req . params , req . body , { } , cb ) ; } ) ; // PUT defineRoute ( "put" , routeURL ( ) , function ( req , store , cb ) { createPasswordHash ( req . body ) ; req . body . _updated = new Date ( ) ; store . update ( req . params , req . body , { multi : true } , cb ) ; } ) ; // PUT with id defineRoute ( "put" , routeURL ( ":_id" ) , function ( req , store , cb ) { createPasswordHash ( req . body ) ; req . body . _updated = new Date ( ) ; store . update ( req . params , req . body , { } , cb ) ; } ) ; // DELETE with id defineRoute ( "del" , routeURL ( ":_id" ) , function ( req , store , cb ) { store . remove ( req . params , { } , cb ) ; } ) ; return server ; }