index.js
function RestApi(config, cb) {
    var self = this;
    self.routes = {};

    self.bindTo = (config && config.bindTo) ? config.bindTo : undefined;
    self.port = (config && config.port) ? config.port : DEFAULT_PORT;

    // create the HTTP server object and on every request, try to match the
    // request with a known route. If there is no match, return a 404 error.
    self.HttpServer = http.createServer(function(req, res) {
        var uriPath;
        var queryStr;
        var urlParts = url.parse(req.url, true);
        if (is.obj(urlParts)) {
            uriPath = urlParts.pathname || undefined;
            queryStr = urlParts.query || undefined;
            debug('queryStr: '+inspect(queryStr));
        } else {
            // no match was found, return a 404 error.
            res.writeHead(400, {'Content-Type': 'application/json; charset=utf-8'});
            res.end('{status:400, message:"Bad URI path."}', 'utf8');
            return;
        }

        // try to match the request & method with a handler
        for (var path in self.routes[req.method]) {
            if (path === uriPath) {
                self.routes[req.method][path](req, res, queryStr);
                return;
            }
        }

        // no match was found, return a 404 error.
        res.writeHead(404, {'Content-Type': 'application/json; charset=utf-8'});
        res.end('{status:404, message:"Content not found."}', 'utf8');
    });

    if (cb) self.listen(cb);
}
function RestApi(config, cb) {
    var self = this;
    self.routes = {};

    self.bindTo = (config && config.bindTo) ? config.bindTo : undefined;
    self.port = (config && config.port) ? config.port : DEFAULT_PORT;

    // create the HTTP server object and on every request, try to match the
    // request with a known route. If there is no match, return a 404 error.
    self.HttpServer = http.createServer(function(req, res) {
        var uriPath;
        var queryStr;
        var urlParts = url.parse(req.url, true);
        if (is.obj(urlParts)) {
            uriPath = urlParts.pathname || undefined;
            queryStr = urlParts.query || undefined;
            debug('queryStr: '+inspect(queryStr));
        } else {
            // no match was found, return a 404 error.
            res.writeHead(400, {'Content-Type': 'application/json; charset=utf-8'});
            res.end('{status:400, message:"Bad URI path."}', 'utf8');
            return;
        }

        // try to match the request & method with a handler
        for (var path in self.routes[req.method]) {
            if (path === uriPath) {
                self.routes[req.method][path](req, res, queryStr);
                return;
            }
        }

        // no match was found, return a 404 error.
        res.writeHead(404, {'Content-Type': 'application/json; charset=utf-8'});
        res.end('{status:404, message:"Content not found."}', 'utf8');
    });

    if (cb) self.listen(cb);
}
function RestApi ( config , cb ) { var self = this ; self . routes = { } ; self . bindTo = ( config && config . bindTo ) ? config . bindTo : undefined ; self . port = ( config && config . port ) ? config . port : DEFAULT_PORT ; // create the HTTP server object and on every request, try to match the // request with a known route. If there is no match, return a 404 error. self . HttpServer = http . createServer ( function ( req , res ) { var uriPath ; var queryStr ; var urlParts = url . parse ( req . url , true ) ; if ( is . obj ( urlParts ) ) { uriPath = urlParts . pathname || undefined ; queryStr = urlParts . query || undefined ; debug ( 'queryStr: ' + inspect ( queryStr ) ) ; } else { // no match was found, return a 404 error. res . writeHead ( 400 , { 'Content-Type' : 'application/json; charset=utf-8' } ) ; res . end ( '{status:400, message:"Bad URI path."}' , 'utf8' ) ; return ; } // try to match the request & method with a handler for ( var path in self . routes [ req . method ] ) { if ( path === uriPath ) { self . routes [ req . method ] [ path ] ( req , res , queryStr ) ; return ; } } // no match was found, return a 404 error. res . writeHead ( 404 , { 'Content-Type' : 'application/json; charset=utf-8' } ) ; res . end ( '{status:404, message:"Content not found."}' , 'utf8' ) ; } ) ; if ( cb ) self . listen ( cb ) ; }
RestApi constructor. Creates the HTTP server objects and starts listening on
the socket.
@param {Object} [config] An optional configuration object to configure the
RestApi. Possible properties are: port and bindTo to specify the listening
port and the address to bind to. If no port is specified the default is 44401
and the default address is INADDR_ANY.
@param {Function} [cb] An optional callback. If there is a callback, the
process will be begin listening on the socket for HTTP requests.
@constructor
RestApi constructor . Creates the HTTP server objects and starts listening on the socket .