Express makes use of HTTP verbs and path patterns to provide a meaningful API for describing routes:
app.verb(path, [callback...], callback)
The route handlers take the same arguments (request
, response
, and next
), and the path can be a string (that will be transformed into a regular expression) or a regular expression.
If we were to define a route handler to update an article, then the request method (verb) would be PUT
, and the URL could look like /articles/211
(where the number represents the article ID). Here's how to do this with Express:
app.put('/articles/:id', function(req, res, next) { var id = req.params.id; console.log('Updating article ' + id); console.log('Attributes: ', req.body); // save to database // send message or render template });