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

What’s happening here:

I’ve spent almost 5 hours on this challenge trying to chain Middleware to create a Time Server but still I cannot progress, I don’t know why, I tried to look for solution in previously asked question like mine, but I can’t get any solution!

All I see is this picture that my 2 tests can’t pass, Any workaround to help me?

Someone also suggested to disable the following line of codes, but It couldn’t work, It seems I am trying to find a solution where it isn’t.

Code I removed…

// Commented out middleware section
app.use("/", function middleware(req, res, next) {
  let str = req.method + " " + req.path + " - " + req.ip;
  console.log(str);
  next();
});

My current codes:

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

Additional

Even at this line of code , double quotes " " on time get removed automatically by after code formatting,"

 time: req.time, 
// instead of 
 "time": req.time,

###My project link(s):

solution: boilerplate-express - Replit

My browser information: (I use Manjaro Linux)

User Agent is: Mozilla/5.0 (X11; Linux x86_64; rv:121.0) Gecko/20100101 Firefox/121.0

Challenge Information:

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

Help me!

Here is the relevant section of code from your repl:

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

...

module.exports = app;

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 + "...");
});
  1. Why do you have a timeout in your ‘/now’ route?
  2. You do not need that server code at the bottom. The server stuff is handled in server.js. You can remove this code block, as it’s throwing an error anyway, which is crashing your repl.

If you modify the above code to remove the timeout and your repl is running but still not passing the FCC tests, it could be because your PC clock is not correctly synced. You can fix this in your PC settings.

I removed the timeout and server code from the myApp.js file and passed the FCC tests with your repl.

2 Likes

I am trying to deal with time, on local machine, to see if i get changes!

After some changes, Neither 1st nor 2nd test can pass

1 Like

Thanks @igorgetmeabrain
It’s hard for me, I tried to deal with my clock but it is not working I don’t know if I need to use same timezone as FCC, Show the good workaround to tackle this problem, I don’t know if I need to change time manually either! Help me with specific solution, Appreciated for the support!

1 Like

There’s an issue with replit at the moment… I’ll get back to you!

2 Likes

Waiting for you after It is done…
Thank you!