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


#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.


#5

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


#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.


#9

try res.send. It will also work too


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