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

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

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

1 Like
#2

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

#3

This does not pass the test as well

#4

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.

1 Like
#5

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

1 Like
#6

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}); 
});
#7

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

#8

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.

#10

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

#11

It’s incorrect! You must assign

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

as specified in challenge instructions.

#12

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 });
})
#13

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()
})

#15

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.

#16

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});
  }       
);
#17

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

1 Like
#18

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
    })
});
#19

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 …

#20

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