Skip to content
Ivaylo Ivanov edited this page Jan 16, 2016 · 4 revisions

Defined in Esrol-router and implemented in Esrol-server-app

About

The Routes are application end points (URIs). Depending on the request protocol, they are two different types - HTTP and HTTPS. Currently, only HTTP is implemented.

Each route can have multiple methods to handle different request types (GET, PUT, POST, DELETE..) for the same end point. Methods can handle requests for single or multiple records and the method's name should start with the request type.

Router

Esrol is using RESTful architecture for implementing web service APIs. The implementation for routing is trough simple string manipulation.

Examples for matching end points:
app/routes/http-routes/posts.js

'use strict';

module.exports = class Posts {

  static get url() {
    return '/posts';
  }

  static getMultipleRecords(req, res) {

  }

  static getSingleRecord(req, res) {

  }

};

www.example.com/posts will match /posts as url property and {type}MulitpleRecords as method
www.example.com/posts/ will match /posts as url property and {type}MulitpleRecords as method
www.example.com/posts// will be 404
www.example.com/posts/1 will match /posts as url property and {type}SingleRecord as method
www.example.com/posts/1/ will match /posts as url property and {type}SingleRecord as method
www.example.com/posts/1// will be 404 www.example.com/posts/1/foo will be 404
www.example.com/posts/foo/bar will be 404

type is the request method type (eg: GET, POST, DELETE etc)

Multiple Records

To take all posts, the route must look like this:

app/routes/http-routes/posts.js

'use strict';

module.exports = class Posts {

  static get url() {
    return '/posts';
  }

  static getMultipleRecords(req, res) {
    // code ..
  }

};

Pay attention on the url property. Each route must exports url property with end point as a string.

Single record

To get the first post only /posts/1, the route must look like this:

app/routes/http-routes/posts.js

'use strict';

module.exports = class Posts {

  static get url() {
    return '/posts';
  }

  static getMultipleRecords(req, res) {
    // code for multiple records..
  }

  static getSingleRecord(req, res) {
    req.record // if the request is posts/1 req.record will evaluate 1
  }

};

As you can see from the example above, you can access the member value from req.record.

Respectively, to consume HTTP DELETE request and delete user with id 18, you must add deleteSingleRecord method

app/routes/http-routes/users.js

'use strict';

module.exports = class Users {

  static get url() {
    return '/users';
  }

  static getMultipleRecords(req, res) {
    // code for multiple records..
  }

  static getSingleRecord(req, res) {
    req.record // if the request is users/12 req.record will evaluate 12
  }

  static deleteSingleRecord(req, res) {
    req.record // if the request is users/18 req.record will evaluate 18
  }

};

404

If there is no route to handle the request, the server will respond with status 404. However, if you want to handle all 404 requests, all you need to do is to create four-oh-four route:

app/routes/http-routes/four-oh-four.js

'use strict';

module.exports = class FourOhFour {

  static get url() {
    return '/fourOhFour';
  }

  static all(req, res) {
    // code ..
  }

};

More features to come in the following releases