loopback/node_modules/resource/README.md

74 lines
1.8 KiB
Markdown
Raw Normal View History

2013-04-09 18:33:29 +00:00
# asteroid.Resource
## About
A `Resource` 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 `Resource` as a sub application. You should never have to write this code since the resource will be created and mounted for you by asteroid.
var asteroid = require('asteroid');
var Resource = require('resource');
var app = asteroid();
var subApp = new Resource({root: '/my-sub-app'});
subApp.mount(app);
subApp.get('/', function (req, res) {
res.send(req.url); // /my-sub-app
});
app.listen(3000);
## resource.app
Each resource is constructed with a asteroid/express sub app at the path provided in the resource's `config.json` options.
### myResource.app.VERB(path, [callback...], callback)
The `myResource.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 `myResource.post()`. See the [Express docs](http://expressjs.com/api.html#app.get) for more info.
**Examples**
myResource.get('/hello-world', function(req, res) {
res.send('hello world');
});
### myResource.app.use([path], middleware)
Use the given middleware function.
**Examples**
// a logger middleware
myResource.use(function(req, res, next){
console.log(req.method, req.url); // GET /my-resource
next();
});
## Config
### Options
#### path
The `asteroid.Route` path where the resource will be mounted.
**Examples**
{
"options": {
"path": "/foo" // responds at /foo
}
}
<!-- ... -->
{
"options": {
"path": "/foo/:bar" // provides :bar param at `req.param('bar')`.
}
}