nodeJS Time Server Help

Tell us what’s happening:
I tried to complete create time server using middleware exercise, but when I submit my code, it keeps saying that it fails “The /now endpoint should return a time that is +/- 20 secs from now”. I even just copy-paste the answer, and it still says that.

Your code so far

const middleware = (req, res, next) => {
  req.time = new Date().toString();

app.get("/now", middleware, (req, res) => {
    "time": req.time

Your browser information:

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

Challenge: Chain Middleware to Create a Time Server

Link to the challenge:

I tried it with your code, and it seems to work fine for me. Just out of curiosity, what timezone is your computer clock set to?

Hi Ganesh, thanks for helping, my timezone is set to US PST.



I honestly don’t know why it’s not working for you. Do you think your glitch project was sleeping when you submitted and it took too long to wake up? Can you try again and see what happens?

The error asks you to show that time +/- 20 seconds from now…
so use the following code, it will work…

function getTheCurrentTimeString() {
var curDate = new Date();
curDate.setSeconds(curDate.getSeconds() + 20);
return new Date(curDate).toString();

app.get(’/now’, (req, res, next) => {
req.time = getTheCurrentTimeString();
}, function (req, res) {
res.send({“time”: req.time});

Assuming the issue is not with your computer time vs glitch server time, the problem may have to do with timezones. Using UTC should fix this issue:

req.time = new Date().toUTCString(); //use UTC time
res.send({time: req.time});