O'Reilly logo

Mastering Web Application Development with Express by Alexandru Vlăduțu

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Express routes

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 ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required