loopback/node_modules/route/README.md

74 lines
1.8 KiB
Markdown

# asteroid.Route
## About
A `Route` inherits from the [asteroid module](../asteroid-module) class. It wraps an asteroid application so that it can be used as a sub application initialized by a configuration file.
This example shows the basic usage of a `Route` as a sub application. You should never have to write this code since the route will be created and mounted for you by asteroid.
var asteroid = require('asteroid');
var Route = require('route');
var app = asteroid();
var subApp = new Route({root: '/my-sub-app'});
subApp.mount(app);
subApp.get('/', function (req, res) {
res.send(req.url); // /my-sub-app
});
app.listen(3000);
## route.app
Each route is constructed with a asteroid/express sub app at the path provided in the route's `config.json` options.
### myRoute.app.VERB(path, [callback...], callback)
The `myRoute.VERB()` methods provide routing functionality inherited from [Express](http://expressjs.com/api.html#app.get), where **VERB** is one of the HTTP verbs, such as `myRoute.post()`. See the [Express docs](http://expressjs.com/api.html#app.get) for more info.
**Examples**
myRoute.get('/hello-world', function(req, res) {
res.send('hello world');
});
### myRoute.app.use([path], middleware)
Use the given middleware function.
**Examples**
// a logger middleware
myRoute.use(function(req, res, next){
console.log(req.method, req.url); // GET /my-route
next();
});
## Config
### Options
#### path
The `asteroid.Route` path where the route will be mounted.
**Examples**
{
"options": {
"path": "/foo" // responds at /foo
}
}
<!-- ... -->
{
"options": {
"path": "/foo/:bar" // provides :bar param at `req.param('bar')`.
}
}