Creating a logger

Inside app.use, we're going to get started by creating a logger that will log out all of the requests that come in to the server. We'll store a timestamp so we can see exactly when someone made a request for a specific URL.

To get started inside the middleware, let's get the current time. I'll make a variable called now, setting it equal to newDate, creating a new instance of our date object, and I'll call it toString method:

app.use((req, res, next) => { var now = new Date().toString(); next();});

The toString method creates a nice formatted date, a human-readable timestamp. Now that we have our now variable in place, we can start creating the actual logger by calling console.log.

Let's call console.log, passing in whatever ...

Get Learning Node.js Development now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.