-
Notifications
You must be signed in to change notification settings - Fork 7
REST Routers
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();
};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.
Type: <Function>
Default: (req, res, next) => res.render()
GET Function (exports.get) will be called when:
-
GETurlis requested, and - 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).
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
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
Type: Object
res.locals is the Express.js' res.locals, with parent context and hash object injected.
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.
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);
}
}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.
Type:
Default: (req, res, next) => res.render()
Same as GET Function, while PUT Function (exports.put) will be called when POST url is requested.
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.
Copyright © 2016 Harttle