Chain Middleware to Create a Time Serve test failing when answer is correct

Tell us what’s happening:

Ive copy/pasted each example on the Hint page, but it still fails every time

Your project link(s)

solution: https://boilerplate-express.timb01.repl.co

Your browser information:

User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 11_2_1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/88.0.4324.150 Safari/537.36.

Challenge: Chain Middleware to Create a Time Server

Link to the challenge:

Can you send us the link to your code as well?

Hi,

I’m having the same issue posted here. I’m not able to pass the “The /now endpoint should have mounted middleware” test.

How did this one get solved?

I’ve tried with both provided solutions and the code below:

app.get("/now",function(req, res, next){
  req.time = new Date().toString();
  next(); 
  },
  function(req,res){
    res.json({"time": req.time});
  }
);

Edit: Code needs to be on myApp.js file and not on server.js

1 Like

im having the same issue

1 Like

I am also seeing the tests fail using repl.it

https://fcc-node.paulywill.repl.co

I am thinking the issue is that if you attempt to carry on with the same code from the previous challenge it breaks the tests.

Try removing the following:



app.use(function middleware(req, res, next) {
  var logString = req.method + " " + req.path + " - " + req.ip;
  
  res.send(logString);
  console.log(logString);
  next();
});
1 Like

can any one please help me what is wrong in the code the condition of ±20 doesn’t gets complete

app.get("/now",function(req, res, next){
req.time = new Date().toString();
next();
},
function(req,res){
res.json({“time”: req.time});
}
);

I had this problem too and I just want to reiterate what @santini44 said:
“Code needs to be on myApp.js file and not on server.js”
This solved my problem.

I was working in the server.js file and applying my .use and .get functions to the app object there. However, the fcc tests look at the myApp object when trying to tell if you chained the middleware correctly. So, I started to create a pull request on their repo that altered it to use app._router.stack instead of myApp._router.stack until I saw @santini44 's comment and realized my issue lol !

If anyone is interested, for learning purposes, if you’re using server.js for the exercises - a workaround for this challenge is possible by adding a line near the bottom of the file that copies the app’s router stack into myApp’s router stack…

var port = process.env.PORT || 3000;
myApp._router = { stack: [...app._router.stack] };
bGround.setupBackgroundApp(app, myApp, __dirname).listen(port, function(){
	bGround.log('Node is listening on port '+ port + '...')
});

… this is because the fcc-express-bground uses the stack object in myApp to see if the “/now” routes stack is the proper length of 2, meaning the middleware is mounted on the .get() route

try {
    //myApp.use(enableCORS);
    app.use('/', myApp);
    var stack = (myApp._router && myApp._router.stack) || [];
    var layers = stack.map((l) => l.name);
.....

// check if /now route has a middleware before the handler
    var nowRoute = stack.filter((l) => {
      if (l.route) {
        return l.route.path === '/now';
      }
      return false;
    });
    if (nowRoute.length > 0) {
      nowRoute = nowRoute[0];
      globals.nowRouteStackLength = nowRoute.route.stack.length;
    }

It’s also possible to get at the stack with myApp.parent._router.stack , but I didn’t spend too much more time on this after I realized where I went wrong :slight_smile:

So, long story short… when the file says “PLEASE DO NOT EDIT THIS FILE” you should probably not edit it :rofl:

I have the same problem, I’m using glitch.com to make the challenges, have you find a solution to this problem?

I am using repl.co

I had an issue with the response falling within the 20 second ± window. I manually adjusted my computer clock to match the output that was displayed on /now and passed the challenge.

3 Likes

yes i did it can my issue has resolve