Basic Node and Express - Chain Middleware to Create a Time Server

Tell us what’s happening:
I am having diffculty identifying where /how this middleware is supposed to be “mounted” to the "/now? endpoint. I have the response working right but my function is not mounted. Please could someone help me out? Here’s my code.

var express = require(‘express’);
var app = express();

function middleware(req, res, next){
req.time = new Date().toString();
console.log(req.method + " " + req.path + " " + “-” + " " + req.ip);
next();
};

app.use(middleware)

app.get(“/now”, middleware, (req, res) => {
res.json({time: req.time})
})

Your project link(s)

solution: boilerplate-express2 - Replit

Your browser information:

User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/107.0.0.0 Safari/537.36

Challenge: Basic Node and Express - Chain Middleware to Create a Time Server

Link to the challenge:

  1. The code should be inside myApp.js

  2. You have used the middleware twice, in two different ways.

This mounts it for all routes that come after it.

app.use(middleware)

This uses the middleware inline on a specific route. The middleware runs before the handler.

app.get('somePath', middleware, (req, res) => )

You only need one of the two. The challenge is asking for the second version. But both work.


Thank you. Because of your comment I was able to pass the test and move on so thanks again.

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.