1.0.0-beta.8 • Published 7 years ago

donode v1.0.0-beta.8

Weekly downloads
1
License
MIT
Repository
github
Last release
7 years ago

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'
}
PropertyTypeDescription
pathstringurl for the route
methodstringcan be one of GET, POST, PUT, UPDATE, DELETE.
handlerstringformat: 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 availale 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',
  }]
}
PropertyTypeDescription
pathstringA route can have params in its path. syntax: {param} example: /user/{id} can respond to /user/1, /user/123 etc..
methodstringvalues are not limited GET, POST, PUT, UPDATE, DELETE. can be any type of request that server can listen to. (HEAD, OPTIONS etc..)
handlerstringA controller can be from a sub directory inside controllers directory. syntax: subdirectory/SomeController@method here in this SomeController is located in app/controllers/subdirectory.
middlewarearray or objectA 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 availale 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.
headersarrayheaders 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 !!!
childrenarrayRoutes 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

PropertyRouteValues
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

MethoddefinitionDescription
sendsend(response_code, responseObject)It takes optional response code and a response object which whill be sent. default: 200 (OK)
rejectreject(response_code, responseObject)takes optional response_code and rejection responseObject. default: 401 (bad request)
setHeadersetHeader(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.

1.0.0-beta.8

7 years ago

1.0.0-beta.7

7 years ago

1.0.0-beta.6

7 years ago

1.0.0-beta.5

7 years ago

1.0.0-beta.4

7 years ago

1.0.0-beta.3

7 years ago

1.0.0-beta.2

7 years ago

1.0.0-beta.1

7 years ago

1.0.0-alpha.4

7 years ago

1.0.0-alpha.3

7 years ago

1.0.0-alpha.2

7 years ago

1.0.0-alpha.1

7 years ago

0.0.2

7 years ago

0.0.0

7 years ago

0.0.1

7 years ago