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

Tell us what’s happening:
Describe your issue in detail here.


I am having troubles passing the test when I hit run it gets stuck in the get /now and is returning me {} I have tried several solutions but the code seems not to be working in Replit
Your code so far

function getTheCurrentTimeString() {
  return new Date().toString();
}

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

Full code:

let express = require('express');
let app = express();

// #1 
// console.log("Hello World")

//#2
/* app.get("/", (req, res) => {
  res.send('Hello Express')
}) */ 

//7 

app.use((req, res, next) => {

 let string = `${req.method} ${req.path} - ${req.ip}`
 console.log(string) 
   
  next();

});

//4

app.use("/public", express.static(__dirname + "/public")) 


//3
 app.get('/', (req, res) => {
  res.sendFile(__dirname + "/views/index.html");
}) 

//5
/* app.get("/json", (req, res) => {
  res.json({
    "message": "Hello json"
  });
*/
//6

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

//8 
function getTheCurrentTimeString() {
  return new Date().toString();
}

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




 module.exports = app;

Your browser information:

User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/114.0.0.0 Safari/537.36

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

Link to the challenge:

Hi there and welcome to the forum!

I think you have a typo here.

Hi igorgetmeabrain, thanks!!! and thank you for checking my code. It works now! I am sorry. I find it weird, I tried with several functions and nothing seemed to work, I restarted my computer and fixed the typo and its working now :slight_smile:

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