restify-abstract-config

0.3.1 • Public • Published

restify-abstract-config

NPM

Install

$ npm install restify-abstract-config

Usage

Use the AbstractAPI class to fast track configuring Restify HTTP methods to your own concrete type's instance methods.

For example:

// $PROJECT_ROOT/app/routes/some-custom-api.js
'use strict';

const AbstractAPI = require("restify-abstract-config").AbstractAPI;

class SomeCustomAPI extends AbstractAPI {
    constructor() {

        super();

        this.addGet('get', '/api/custom/:id', this.get);
        this.addGet('getAll', '/api/custom', this.getAll);

        this.addPut('update', '/api/custom/:id', this.update);

        this.addDelete('delete', '/api/custom/:id', this.delete);

        this.addPost('create', '/api/custom', this.create);

    }

    get(req, res, next) {
        res.send('get');
        next();
    }

    getAll(req, res, next) {
        res.send('getAll');
        next();
    }

    update(req, res, next) {
        res.send('update');
        next();
    }

    delete(req, res, next) {
        res.send('delete');
        next();
    }

    create(req, res, next) {
        res.send('create');
        next();
    }

}

module.exports = new SomeCustomAPI();

What is important here is that the server in following code snippet is the instance you get after executing createServer() on the restify module (This is showcased in another code snippet below).

Execute the setup of your routes above as follows (of course you can invoke this as you see fit in your own code base):

// $PROJECT_ROOT/app/routes/index.js
'use strict';

const someCustomAPI = require('./some-custom-api');

module.exports = {

    setup: function(server) {

        someCustomAPI.setupRoutes(server);

        //Print this API's endpoints
        someCustomAPI.endpointsInfo();

        //Print all the Restify-configured routes
        someCustomAPI.allRoutesInfo(server);

    }

}

If you want the version of your project's package.json available at an endpoint as well, be sure to provide a second argument to the above setupRoutes function:

someCustomAPI.setupRoutes(server, '/api/version');

You can use an endpoint URL of your choice.

Ensure that the above setup function is called again eg:

// $PROJECT_ROOT/server.js
'use strict';

let restify = require('restify');
let routes = require('./app/routes');

let server = restify.createServer();
routes.setup(server);

server.listen(8080, function() {
    console.log('%s listening at %s', server.name, server.url);
});

License

MIT

Bugs

See https://github.com/0v3rst33r/restify-abstract-config/issues.

Readme

Keywords

none

Package Sidebar

Install

npm i restify-abstract-config

Weekly Downloads

0

Version

0.3.1

License

MIT

Unpacked Size

12.9 kB

Total Files

7

Last publish

Collaborators

  • 0v3rst33r