Skip to content
This repository was archived by the owner on Apr 14, 2026. It is now read-only.

REST Routers

Jun Yang edited this page Nov 15, 2016 · 5 revisions

This article provides all you want about Brick.JS REST Routers.

Brick.JS REST Routers defines the routing URL and view function for the brick. The Default REST Router File is router.js within each brick. The filename is configurable via config.server, see: https://github.com/brick-js/brick.js#server

Below is a simple router file:

exports.url = '/';
exports.get = function(req, res, next){
    res.render({ title: 'Hello World'});
};
exports.post = function(req, res, next){
    // create sth.
    res.status(201);
    res.render({ title: 'Hello World'});
};
exports.put = function(req, res, next){
    // update sth.
    res.status(200);
    res.render({ title: 'Hello World'});
};
exports.delete = function(req, res, next){
    // delete sth.
    res.status(204);
    res.end();
};

URL

Type: <String> or <RegExp> or <Array>

Optional: if not set, this module will not be routed (Typically for template partials).

The exports.url can be any URL format that Express Router accepts. Besides String, exports.url could be an Array, Parameter Capture, and Regular Expressions:

exports.url = ['/', '/homepage'];
exports.url = '/posts/:id';
exports.url = /^\/commits\/(\w+)(?:\.\.(\w+))?$/

Captured params are accessible via req.param() and req.params, see: http://expressjs.com/en/4x/api.html#req

Internally, Brick.JS use Express for routing.

GET Router

Type: <Function>

Default: (req, res, next) => res.render()

GET Function (exports.get) will be called when:

  1. GET url is requested, and
  2. Current brick is included/extended by other bricks' view.html (See: Partials and Layouts).

exports.get is supposed to do some business logic, and finally resolve template context for view.html of current brick.

The context finally passed into template is _.assign({}, req.app.locals, parentCtx, res.locals, ctx).

req

Type: Object

The req is exactly an Express Request with properties: req.params, req.query, and so on.

See: http://expressjs.com/en/api.html#req

res

The res is exactly an [Express Response][express-response] with properties like res.send, res.status, res.redirect except res.render.

See: http://expressjs.com/en/api.html#res

res.locals

Type: Object

res.locals is the Express.js' res.locals, with parent context and hash object injected.

res.render

Type: Function(<context>)

Should be called when context is resolved without error. Async resolving is supported.

The context object with which res.render is called will be merged into parent context, and rendered into template file (Typically, view.html).

Parent context exists when you're using template partials or layouts, see Partials and Layouts.

next

Type: Function(<Error>)

next can be called whenever there's an error. For Example:

function(req, res, next){
  if(req.user)  res.render({user: req.user});
  else{
    var err = new Error('user not found!');
    err.status = 404;
    next(err);
  }
}

POST Router

Type: <Function>

Default: (req, res, next) => res.render()

Same as GET Function, while POST Function (exports.post) will be called when POST url is requested.

PUT Router

Type:

Default: (req, res, next) => res.render()

Same as GET Function, while PUT Function (exports.put) will be called when POST url is requested.

DELETE Router

Type: <Function>

Default: (req, res, next) => res.render()

Same as GET Function, while DELETE Function (exports.delete) will be called when DELETE url is requested.

Clone this wiki locally