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

Debugging routes and middleware


When we start working on an application that's already being developed, one of the first things we want to find out is what are the routes and middleware functions defined. In general, we should be able to deduce that right away from a server.js file for example, but some applications are more complex than that and might load middleware from different files.

To check out what routes and middleware functions are present on the stack, we can iterate through the app._router.stack object, as shown in the following example:

var express = require('express');
var app = express();
var inspect = require('util').inspect;
var morgan = require('morgan');

var users = [{ name: 'John Doe', age: 27 }];

app.use(morgan());

app.route('/users').get(function(req, res, next) {
  res.send(users);
}).post(function(req, res, next) {
  res.send('ok');
});

app._router.stack.forEach(function(item) {
  if (item.route) {
    console.log('Route: %s', inspect(item.route, { depth: 5 })...