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

Tell us what’s happening:
Describe your issue in detail here.
I do not understand the reason why this does not work.

require('dotenv').config()
let express = require('express');
let app = express();
let absolutePath=__dirname + "/views/index.html"

app.use("/",function(req,res,next){
  console.log(req.method + " " + req.path + " - " + req.ip);
  next();
});

app.use(express.static(__dirname + "/public"))
app.get("/",(req,res)=>{
  res.sendFile(absolutePath)
})

app.get("/json",(req,res) => {
  if(process.env.MESSAGE_STYLE === "uppercase"){
    res.json({ "message": "HELLO JSON" })
  }
    res.json({"message":"Hello json" })
   
});

app.get('/json', (req, res) => {
  process.env.MESSAGE_STYLE === 'uppercase' ? 
    res.json({ "message": "HELLO JSON" }):
    res.json({ "message": "Hello json" })
})

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




Your project link(s)

solution: boilerplate-express-2 - Node.js Repl - Replit

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/105.0.0.0 Safari/537.36

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

Link to the challenge:

Hello,

I guess the issue is with this line :

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

next() enables you to go to the next middleware (I think, if I am wrong please someone correct it), but there isn’t any. You should put the 2nd function in a new middleware.

But regarding what is done in the first, try to do this :

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

Or only res.json(req.time), according to the format you want to get

1 Like

I too might be wrong here but if we just have the res.json() in the middleware then there is no chaining happening? I’m fairly certain I’m on the same boat as u/santiagodeleondecar1, which is we are trying to run it locally and some of the answers just aren’t working correctly. My code is pretty much exactly like santiago’s and I haven’t gotten my code to run for 2 days. My time is returned as a string and in the correct format and it updates the time for every new request. I’ve even tried submitting previous URL’s [http://localhost:3000 | http://localhost:3000/json | http://localhost:3000/now] with no luck. Unless someone see’s that we’re making an obvious express error then I believe it’s a Node.js update messing with stuff, the FCC answer code isn’t able to work properly either due to our personal system settings or browser settings. Any more input or suggestions would be much appreciated! :slight_smile:

1 Like

The code in the initial post is missing the object {} in the response

res.json({ time: req.time })

but other than that the code should be passing.

We can’t really debug your code without seeing it but it is possible that it is a system issue. Make sure your system time is correct (all OSs can sync the time with some time server). You can also look in the network tab when you do the submission and check the response.

1 Like

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.