7 RESTful routes
CRUD stands for Create, Read, Update and Destroy. REST is a mapping between HTTP routes and CRUD.
| name | url | verb | desc | Mongoose method |
|---|---|---|---|---|
| INDEX | /dogs | GET | Display a list of all dogs | Dog.find() |
| NEW | /dogs/new | GET | Display form to make a new dog | N/A |
| CREATE | /dogs | POST | Add new dog to DB | Dog.create() |
| SHOW | /dogs/:id | GET | Show info of a dog, should be after NEW | Dog.findById() |
| EDIT | /dogs/:id/edit | GET | Show edit form for one dog | Dog.findById() |
| UPDATE | /dogs/:id | PUT | Update a particular dog, then redirect | Dog.findByIdAndUpdate() |
| Destroy | /dogs/:id | DELETE | Delete a particular dog, then redirect | Dog.findByIdAndRemove() |
PUT in HTML form
HTML <form> cannot send a request other than GET and POST, like PUT.
In order to solve this in Node, first install method-override package. Then use form header as:
<form action="/blogs/<%= blog._id %>?_method=PUT" method="POST">
</form>
var methodOverride = require("method-override");
app.use(methodOverride("_method"));
app.put("/blogs/:id", function(req, res) {
Blog.findByIdAndUpdate(req.params.id, req.body.blog, function(err, updatedBlog) {
// if err
});
});
