Skip to content

Super fast & Lightweight node framework to build RESTful APIs.

License

Notifications You must be signed in to change notification settings

madhusudhand/donode

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

82 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

donode

Superfast & Lightweight node framework for building RESTful APIs.

Simple . Flexible . High Performance . ES2015+

Enables developers to focus on writing reusable application logic in a highly modular approach.

Use donode-cli, to get started.

**NOTE: Requires node 8.0 or higher **

High Performance

Here are the performance results.

Documentation

App Structure

Create the app using donode-cli, it is as simple and modular as

  • app
    • controllers
    • middleware
    • headers.js
    • routes.js
  • env
    • development.env.js
    • production.env.js
  • app.js

Routing

One Place for all your routes and they go into app/routes.js.

A Route can be as simple as

{
  path: '/hello',
  method: 'GET',
  handler: 'HelloController@get'
}
Property Type Description
path string url for the route
method string can be one of GET, POST, PUT, UPDATE, DELETE.
handler string format: SomeController@method
Here in this format SomeController is the controller (a class) located in app/controllers directory.

And method (name after @) is the name of the method in the given controller, which gets called and send response.

more details available in controllers section.

Advanced Routing

A Route can take options like middleware, headers, children.

{
  path: '/user/{id}',
  method: 'GET',
  handler: 'UserController@get',
  middleware: ['Auth'],
  headers: ['allow-cors'],
  children: [{
  	path: '/settings',
    method: 'GET',
    handler: 'UserController@settings',
  }]
}
Property Type Description
path string A route can have params in its path.
syntax: {param}

example:
/user/{id} can respond to /user/1, /user/123 etc..
method string values are not limited GET, POST, PUT, UPDATE, DELETE.
can be any type of request that server can listen to. (HEAD, OPTIONS etc..)
handler string A controller can be from a sub directory inside controllers directory.
syntax: subdirectory/SomeController@method
here in this SomeController is located in app/controllers/subdirectory.
middleware array
or
object
A Middleware is an intermediate handler which can be used to perform some pre-checks such as Auth.
It is a class located in app/middleware directory and will be called before the handler method.

Array Syntax: ['Middleware1', 'Middleware2']

A list of strings which are the names of the classes from app/middleware directory. When attached to a route, these will be called in the given order before the actual handler.

more details available in middleware section.

Object syntax:
{
   all: ['Middleware1'],
   only: ['Middleware2'],
   children: ['Middleware3']
}


-- all: attached to current the route and all its children.
-- only: attached only to current route.
-- children: attached to all the children, but not to current route.

Note: All are optional. Any of them can be given/ignored.

Order: middleware defined under all, children which are coming from parent routes (if any), then all, only of current route.
headers array headers property allows to attach a set of headers that can to be sent along with the response for every request.
It takes an array of stings which are defined in app/headers.js.

syntax
['allow-cors', 'json-content']

Note: currently headers attached to a route will apply only to it. Not to children. Object syntax is yet to come !!!
children array Routes can be nested with this attribute.
This takes list of sub-routes which takes same set of above properties.

example route config
{
  path: '/user/{id}',
  method: 'GET',
  handler: 'UserController@get',
  children: [{
     path: '/settings',
     method: 'POST',
     handler: 'UserController@settings'
  }]
}

this will register the routes
GET: /user/{id}
POST: /user/{id}/settings

Controllers

app/controllers: A place for all controllers of the app.

A controller is a class which inherits Controller from donode, and has the handler methods of a route.

const Controller = require('donode').Controller;

class HomeController extends Controller {
  constructor() {
    super();
  }

  get(request, response) {
    response.send({
      'app': 'works !!!'
    });
  }
}

module.exports = HomeController;

The method get(request, response) will be the handler for a route.

handler: 'HomeController@get'

It gets request, response as params.

Request

It is a native Node Request object. Along with default properties it also contains

Property Route Values
query /user?p1=123&p2=donode { p1: 123, p2: 'donode' }
params /user/{id} { id: 123 }
body - payload sent along with request
headers - headers sent along with request
url - { }
originalUrl /user/{id} /user/123
method - GET, POST, PUT, DELETE etc..

Response

It is a native Node Response object. Along with default properties it also contains

Method definition Description
send send([response_code,] responseObject) It takes optional response code and a response object which whill be sent.

default: 200 (OK)
reject reject([response_code], responseObject) takes optional response_code and rejection responseObject.

default: 401 (bad request)
setHeader setHeader(type, value) takes header type and value

Middleware

const Middleware = require('donode').Middleware;

class Auth extends Middleware {
  constructor() {
    super();
  }

  handle(request, response, next) {
    // do some auth validation here

    // if (not authorized)
    // return response.send(401, { some: 'error data' });

    // else (proceed to the controller hander)
    return next();
  }
}

module.exports = Auth;

Note:

When response.send() or response.reject() are called in a middleware, the call chain stops and handler method will not be called.

Environments

Environments for the app can be managed by creating corresponding configuration file

env/.env.js

This configuration can be used with NODE_ENV while running the app.

$ NODE_ENV= node app.js

Examples:

NODE_ENV=production node app.js
NODE_ENV=stage node app.js

Without NODE_ENV it defaults to development.env.js.

About

Super fast & Lightweight node framework to build RESTful APIs.

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published