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

restier

v0.0.9

Published

Express pluggable REST API, built in the right way.

Downloads

5

Readme

restier

Build Status Issues GitHub forks GitHub stars GitHub license

Express pluggable REST API, built in the right way. It always use a common convention in API responses, making the usage ideal for fast and secure development. You can use Promises, Callbacks, all what you want in single method call. It also supports MVC, using fancy controllers.

Features

  • Express pluggable middleware.
  • Common API response, using proper convention.
  • Blazing fast development.
  • Superb performance, due to its minimalist approach.
  • Single point for responses, using dispatch method.
  • Response support for plain objects, errors, callbacks and promises; suitable for all kind of servicing approach.
  • Automatic exception capture, avoiding unexpected exceptions on production.
  • High security with helmet support.
  • Avoid all kind of chatty responses, hiding server tokens.
  • Support for controllers, making architecture and maintenance better and frictionless.
  • Understandable samples.

Quick Start

var restier = require('restier');
var api = restier();
var router = api.router;
var Promise = require('bluebird');

// Sample returning object
router.get('/hello', function (req, res, next) {
  res.dispatch({hello: 'World'});
});

// Sample returning exception
router.get('/error', function (req, res, next) {
  res.dispatch(new Error('Upps, this got an error.'));
});

// Exception capturing, what????
router.get('/exception', function (req, res, next) {
  throw new Error('Wow, this blows up.')
});

// Promise support, ohh this looks useful
router.get('/promise', function (req, res, next) {
  var promise = new Promise(function (resolve, reject) {
    resolve({hello: 'world'});
  });
  res.dispatch(promise);
});

// Support for legacy callbacks, uff I can't loose my async approach.
router.get('/callback', function (req, res, next) {
  var callback = function(done) {
    done(new Error('Upps!!, I got an error'));
  };
  res.dispatch(callback);
});

// And what about controllers???
router.use('/', api.useControllers(__dirname + '/../controllers'));
// You'll have to dig more on this documentation, and samples. What are you waiting for?

var app = express();
// Yes you can plugit in your express app, even in a route.
app.use('/api/v1', api.app);

app.listen(4000);

Install

$ npm install restier

API

var restier = require('restier');
var api = restier();

router

Returns the express router in which you must register the API routes.

Example

var router = api.router;
router.get('/hello', function (req, res, next) {
  res.dispatch({hello: 'World'});
});

Constructor ([options])

Returns restier object.

Options

restier() accepts these properties in the options object.

poweredBy

Value for X-Powered-By header. Defaults to false.

compression

Options for compression module. Defaults to false, or compression disabled.

helmet

Options for helmet module. If false passed, helmet will be disabled. If true is passed helmet will be configured with helmet defaults. Otherwise helmet will be configured with your options.

cors

Options for cors module. Defaults to false, or cors disabled.

bodyParser

Options for bodyParser module. If configuration is specified it will be merged with restier default options described below.

var defaultOptions = {
        json: {
            limit: '100kb',
            strict: true
        },
        urlencoded: {
            extended: true,
            limit: '100kb',
            parameterLimit: 150
        }
    };
methodOverride

Key to define header in which server must read real HTTP method to override actual method. It lets you use HTTP verbs such as PUT or DELETE in places you normally can't. Defaults to _method.

router

Returns the express router in which you must register the API routes.

Example

var router = api.router;
router.get('/hello', function (req, res, next) {
  res.dispatch({hello: 'World'});
});

app

Returns an express app, to mount as middleware in route, or directly binds to interface.

Example

var app = express();
app.use('/api/v1', api.app);
app.listen(4000);

useControllers(controllersDir)

Returns a restier router, with routes mounted for controllers located in the specified directory. This method uses realpath for determining the correct path for looking out controllers.

router.use('/', api.useControllers(__dirname + '/controllers'));

Controller Example

var Promise = require('bluebird');
var Controller = require('restier').Controller;

function HomeController(router) {
    Controller.call(this, router);
}

HomeController.prototype.get = function() {
    return {hello: 'World'};
}

HomeController.prototype.getItem = function(id) {
    var request = this.request;
    var promise = new Promise(function (resolve, reject) {
        resolve({hello: id, q: request.query});
    });

    return promise;
}

module.exports = HomeController;

Tests

This is a work in progress

To run the test suite, first install the dependencies, then run npm test:

$ npm install
$ npm test

People

The original author of Restier is Carlos Luis Castro Márquez

License

MIT