Router
Restful has a minimal router that only handles accepting the route and calling a controller. It currently only accepts HTTP requests but in the future it could also serve as a main source for Meteor methods and publications. It provides functions in the module namespace Restful.Router
.
We recommend using ES6 de-structuring in order to reduce the noise that the module namespaces add. For example, const {get} = Restful.Router
will allow you to reference get
in that file. We'll show the de-structured version in the examples below.
const {acceptJSON, applyMiddleware, scope, get, post, put, delete_} = Restful.Router;
applyMiddleware(
acceptJSON,
yourConnectMiddleware,
);
scope('/v1', () => {
resources("/posts", ItemController, {except: 'delete'})
get('/items', ItemController, 'index')
post('/items', ItemController, 'create')
get('/items/:id', ItemController, 'show')
put('/items/:id', ItemController, 'update')
delete_('/items/:id', ItemController, 'delete')
});
Restful.Router.get
Accept HTTP GET requests
const {get} = Restful.Router
get('/items', ItemController, 'index');
resources
To reduce boilerplate you can also use a resources
function that uses get, put, post, delete controller actions. Note, these are not created automatically so you would have to add those manually (see Controllers).
// use all actions (get, put, post, delete)
resources('posts', PostController)
// use only put action
resources('posts', PostController, {only: 'put'})
// use all except update & delete
resources('posts', PostController, {except: ['update', 'delete']})
If you'd like to remove
You can optionally scopes to make API versioning easier.
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Praesent nec massa tristique arcu fermentum dapibus. Integer orci turpis, mollis vel augue eget, placerat rhoncus orci. Mauris metus libero, rutrum eu ornare ut, molestie vel nunc. Morbi sed iaculis metus.
Updated less than a minute ago