Tell us what’s happening:
const middleware=(req,res,next)=>{
req.time= new Date().toString()
next()
}
app.get(“/now”,middleware,(req,res)=>{
res.send({ time: req.time })
})
this is code i have written but it is giving an error of The /now endpoint should have mounted middleware
is any one have answer for it
###Your project link(s)
solution: https://3000-freecodecam-boilerplate-6y5wzhrcz0a.ws-us117.gitpod.io
Your browser information:
User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/132.0.0.0 Safari/537.36
Challenge Information:
Basic Node and Express - Chain Middleware to Create a Time Server
Welcome to the forum! Try returning JSON.
Your code is passing for me. Share your workspace so we can see all the code.
Does using toUTCString
change anything for you?
As an aside, Express will convert an object to JSON when using send.
res.send([body])
When the parameter is an Array or Object, Express responds with the JSON representation:
1 Like
As an aside, Express will convert an object to JSON when using send.
Thanks for clarification about res.send()
! I recently completed the Basic Node and Express challenges and in most of them I used res.json()
. Based on your input, I looked up what the difference was. Turns out res.send()
is more versatile because it changes content-type based on what it’s given. So, if you pass it a string, the content-type
is text/html
, but if you pass it an object the content-type
becomes application/json
.
Well, there is nothing wrong about being explicit about it.
It could also read slightly better if you send different payloads that you can tell what it is, by the method. It would likely also give better code searching if you can search for the method.