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

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

What do I need to do to return time between ±20 seconds

5 Likes

app.get(’/now’, function(req,res, next){
next();
}, function(req, res){
var time = new Date().toString();
console.log(‘time’+time);
res.json({‘time’: time});
}
);

This does not pass the test as well

For me the code I provided passed the test:

app.get('/now', function(req,res, next){
  
  next();
}, function(req, res){
 var time = new Date().toString();
  console.log('time'+time);
  res.json({'time': time});
}
       );

Make sure you copy-paste it correctly.

2 Likes

Had the same problem. My computers time is 2h off. Thats why the the test didn`t pass.
Might be your problem aswell?

3 Likes

I don’t understand what I did wrong, can someone help me out here? Thanks!

This is the error, apparently the time result is correct

// running test
"/now" route has no mounted middleware
// tests completed

This is the code

app.get('/now', function(req, res, next) {
  req.time = new Date().toString();
  console.log('middle ware req.time = ' + req.time);
  next();
},function(req, res) {
  res.json({time: req.time}); 
});
1 Like

u have to return a json object… use res.json to return {time: “req.time”}

Yes I also faced this problem, solved it by manually putting my date and time in the new Date(“12 July 2018 00:53:10”) and submitted it close to this time on my pc clock and it passed.

2 Likes

It is no correct! This return a string on a time field

It’s incorrect! You must assign

req.time = new Date().toString()

as specified in challenge instructions.

This is the code i’ve tried and it’s not working… Any idea why?

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

app.get(’/now’, function(req, res, next) {
req.time = new Date().toString() // Hypothetical synchronous operation
next();
}, function(req, res) {
res.send({time: req.time}); // res.send()
})

For any of those hustlers whose code is not working, here’s my code which is working perfectly:

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

I hope this will help you out.

Hey I passed this test:

To pass it, put this code in myApp.js file and not in server.js file:

/** 8) Chaining middleware. A Time server */
app.get('/now', function(req, res, next){
  req.time = new Date().toString();
  next();
},
  function(req, res) {
    res.send({"time": req.time});
  }       
);
3 Likes

In my case, I set the time on my computer be the closest to the one showed in the /now page :slight_smile:

2 Likes

For them to work, just update your machine’s time to match the one on internet (in your timezone), or you can hard code machine’s time into new Date(‘create date time object here’) //wc is harder coz you have to match ± 20 secs. I’m using Heroku (ditched glitch coz it has some serious issues). So I added TZ global variable (thats the link: dev.to/pauxdsantamaria/change-the-timezone-on-a-heroku-app-2b4) to my global variables and set my machine`s time to auto update. It feels like magic seeing that green check mark suddenly flashed before me :grin:.
Thats my solution:



app.get('/now', (req, res, next)=>{
    req.time = new Date().toString();
    next();
}, (req, res)=>{
    res.send({
        "time": req.time
    })
});
2 Likes

Exactly. It is a question of making sure that your time is closest to the one used by new Date().toString() which is GMT+0000 (UTC).

I mean ensure the time in your computer is aligned with one displayed by GMT and UTC …

1 Like

I set the time on your computer be the closest to the one showed in the /now page or let your computer detect it selft the date and time

1 Like

Not the best exercise if you have to hack your computer time zone for it to work…

7 Likes

this seem to work for me.

req.tim = Date().toUTCString()