-
Notifications
You must be signed in to change notification settings - Fork 0
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Future #1
Comments
Thanks, Anachron! Yes, this project is still maintained. Would you like to contribute? Does it need something? |
Yes, I would like to have something like rest.all('/api/food', function(req, rest, action, url) {
// action is based on HTTP header, get/put/post/delete
switch( action ){
case 'index': // /api/food
rest.ok(records);
break;
case 'view': // /api/food/:id
var record = records[req.params.id];
if (record) rest.ok(record);
else rest.notFound();
break;
case 'create': // /api/food
records[req.params.id] = req.body;
return rest.accepted(url + encodeURI(req.params.id));
case 'update': // api/food/:id
records.push(req.body);
rest.created(url + (records.length - 1));
break;
case 'delete': // /api/food/:id
delete records[req.params.id];
rest.gone();
break;
}
}); Does it make sense? For me, this is more readable. Also I would only have to send the basic URL, the rest the application will handle. |
So in your sample, you route /api/food to the handler, but it wouldn't account for /api/food/5. You'll need to split them up. Also your actions aren't restful... They should be HTTP verbs. So how about this syntax? rest.all('/api/food', function(req, rest) {
switch( req.method ){
case 'get': // /api/food
rest.ok(records);
break;
case 'post': // /api/food
records[req.params.id] = req.body;
return rest.accepted(req.url + encodeURI(req.params.id));
default:
rest.notImplemented();
break;
}
});
rest.all('/api/food/:id', function(req, rest) {
switch( req.method ){
case 'get':
var record = records[req.params.id];
if (record) rest.ok(record);
else rest.notFound();
break;
case 'put':
records.push(req.body);
rest.created(req.url + (records.length - 1));
break;
case 'delete':
delete records[req.params.id];
rest.gone();
break;
default:
rest.notImplemented();
break;
}
}); |
Actually you can have an optional parameter, so you could handle everything in a single handler rest.all('/api/food/:id?', function(req, rest) {
switch( req.method ){
case 'get': // /api/food
if (req.params.id) {
var record = records[req.params.id];
if (record) rest.ok(record);
else rest.notFound();
} else {
rest.ok(records);
}
break;
case 'post': // /api/food
records[req.params.id] = req.body;
return rest.accepted(req.url + encodeURI(req.params.id));
case 'put':
records.push(req.body);
rest.created(req.url + (records.length - 1));
break;
case 'delete':
delete records[req.params.id];
rest.gone();
break;
default:
rest.notImplemented();
break;
}
}); |
Hi there, is this project still maintained? I kinda like it and I would like to know if you are still working on it.
The text was updated successfully, but these errors were encountered: