npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

json-rest-api

v0.1.1

Published

A simple lightweight REST API that receives and responds to JSON HTTP requests, supports all verbs. Very simple.

Downloads

17

Readme

json-rest-api

A lightweight REST API that receives and responds to JSON HTTP requests, supports all verbs. Expects JSON requests and responds in JSON via an added method to the http.ServerResponse object called json (see below).

Motivation

Sometimes you want a simple REST API and don't need all the bells and whistles of express.js.

Installation

npm install json-rest-api

Example

server

'use strict';
var JsonRestApi = require('json-rest-api');
var inspect = require('util').inspect;
var debug = require('debug')('json-rest-api:example:server');

var RestApi = new JsonRestApi({port: 8000}, function(err) {
    if (err) {
        debug('/ping error: '+inspect(err));
        return;
    }
    console.log('Listening on port 8000.');

    // add a "GET" route
    RestApi.addRoute('get', '/ping', function(req, res, json, qs) {
        var qsExp = {'alpha': 'one', 'beta': '2', 'gamma': 'true'};
        assert.deepEqual(qsExp, qs);
        res.json({success: true, pong: 'pong'});
    });

    // add a "POST" route
    RestApi.addRoute('get', '/setName', function(req, res, json, qs) {
        var qsExp = {'alpha': 'one', 'beta': '2', 'gamma': 'true'};

        assert.deepEqual(qsExp, qs);
        res.json({success: true, pong: 'pong'});
    });
});

client

'use strict';
var request = require('request');
var inspect = require('util').inspect;
var debug = require('debug')('json-rest-api:example:client');
var assert = require('assert');

var qs = {
    alpha: 'one',
    beta: 2,
    gamma: true
};

request({json:true, qs:qs, uri: 'http://localhost:8000/ping'}, function(err, res, json) {
    if (err) {
        debug('Error: '+inspect(err));
        return;
    }

    assert.ok(json.success === true);
    assert.ok(json.pong === 'pong');
    console.log('Success!');
});

request({json:true, qs:qs, uri: 'http://localhost:8000/setName'}, function(err, res, json) {
    if (err) {
        debug('Error: '+inspect(err));
        return;
    }

    var qsExp = {'alpha': 'one', 'beta': '2', 'gamma': 'true'};

    assert.ok(json.success === true);
    assert.ok(json.cmd === 'setName');
    assert.deepEqual(json.qs, qsExp);
    console.log('Success!');
});

API

RestApi([config], [cb])

RestApi constructor. Creates the HTTP server objects and starts listening on the socket.

Params:

  • Object [config] Optional configuration object to configure the server. The configuration options are:
    • bindTo - Optional. The address to bind, default: INADDR_ANY.
    • port - Optional. The port to bind the server, default: 44401.
  • Function [cb] An optional callback. If there is a callback, the process will be begin listening on the socket for HTTP requests. Without a callback, you must call listen explicitly.

listen([cb])

Start listening on the socket for HTTP requests.

Params:

  • Function [cb] An optional callback called when the server is ready.

stop([cb])

Stops the server from acccepting new connections and listening on the port.

Params:

  • Function [cb] The callback function. Optional.

addRoute(verb, path, cb)

Add a route along with a function to run.

Params:

  • String verb HTTP verb for route. e.g.: 'get', 'post'.
  • String path A valid URI path.
  • Function cb A function to run when the path executes. Which has the following form: fn(req, res, json, qs): * @param {Object} req - Node.js HTTP request object * @param {Object} res - Node.js HTTP response object * @param {Object} json - The JSON body of the request, if present. * @param {Object} qs - The query string of the request, if present.

Return:

  • Boolean|Error True, if no errors on the parameters, Error
  • Error|Boolean true on success and Error on failure.

json([status code,] jsonObject)

A method for the JsonServerResponse prototype to send JSON by passing an object.

Params:

  • Number|Object Optional. First argument may be either the status code or the object to convert to JSON.
  • Object Optional. If the first object is the status code, then the second param must be the object to convert to JSON.

LICENSE

The MIT License (MIT)

Copyright (c) 2013,2014 Edmond Meinfelder

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.