Book Image

Mastering Web Application Development with Express

By : Alexandru Vladutu
Book Image

Mastering Web Application Development with Express

By: Alexandru Vladutu

Overview of this book

Table of Contents (18 chapters)
Mastering Web Application Development with Express
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
Index

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 render template
});

Specifying the path

The path parameter can be either a string (transformed into a regular expression internally by Express) or a regular expression, in case we need to match very specific patterns (such as an e-mail address...