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

Tell us what’s happening:

I m unable to understand what do I need to do in next step, In my middleware I am trying to log out a value, is that right? but if that is the case, why is not logging out and what is next?

Your code so far

let express = require('express');
let app = express();
require('dotenv').config();

app.get("/", function(req,res){
    res.sendFile(__dirname + "/views/index.html")
})

// app.use("/public", express.static(__dirname + "/public"))
app.use(function(req, res, next){
    res.send( console.log(req.method +" "+ req.path +" - "+ req.ip));
})
 module.exports = app;
app.get("/now", function(req,res,next){
    res.send(console.log({time: req.time}))
})

Your browser information:

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

Challenge Information:

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

Hi, @mahassan ,

In your code I can not see the implementation of the pattern as recommended by the exercise:

app.get('/user', function(req, res, next) {
  req.user = getTheUserSync();  // Hypothetical synchronous operation
  next();
}, function(req, res) {
  res.send(req.user);
});

Could that be the reason? Try to think of a “middleware” as a functionally that must run on the backend before reporting something to the user.

Let us know if this helps.

EDIT:

I saw your question more carefully, @mahassan . Sorry I didn’t before.

next() means: leave this (middleware) handler (as described by one of the moderators below), go to the next one.

There is more useful information clarifying the exercise in the following posts.

Happy coding!

also this should be the last thing in the file

  • You are supposed to res the value, not console.log it.

  • You need two handlers, a middleware handler and a final handler.

  • app.METHOD('/somePath', middlewareHandler, finalHandler)

  • The middleware handler should set the property value and then call next, the final handler should res the value of the property using the required structure {time: req.time}

Example:

app.get(
  "/name",
  (req, res, next) => {
    req.name = "John Doe";
    next();
  },
  (req, res) => {
    res.json({ name: req.name });
  }
);

http://localhost:3000/name returns {"name":"John Doe"}

I am not sure I follow, from what I understood is that middleware is function that runs and what we do in it, is ask the backend to get us something like @evaristoc said and next means to skip middleware and go to handler? Following is my code can you help me figuring out what I am missing, right now learning BE is too confusing

let express = require('express');
let app = express();
require('dotenv').config();

app.get("/", function(req,res){
    res.sendFile(__dirname + "/views/index.html")
})

// app.use("/public", express.static(__dirname + "/public"))
app.use(function(req, res, next){
    res.send( console.log(req.method +" "+ req.path +" - "+ req.ip));
})
app.get("/now", function(req,res,next){
    req.time
}, ()=>{
    {time: req.time}
})
 module.exports = app;
  • You have to set req.time in the middleware handler to the date as required.

  • You have to call next() in the middleware handler so it passes control.

  • The final handler callback should be provided req,res, that is how it has access to the value set on req in the middleware.

  • In the final handler, you have to res the value you set in the middleware handler.

app.get(
  "/somePath",
  (req, res, next) => {
    // middleware handler
	// set some value
	// passes control to the next handler
    next();
  },
  (req, res) => {
    // runs after next() is called
	// res some value set in middleware
  }
);

next hands off control to the next handler.

You can add as many handlers you like. They each need to pass control to the next handler by calling next()

Nonsensical example, but it proves the point:

app.get(
  "/user",
  (req, res, next) => {
    req.name = "John Doe";
    next();
  },
  (req, res, next) => {
    req.age = 42;
    next();
  },
  (req, res, next) => {
    req.email = "jodo@test.com";
    next();
  },
  (req, res) => {
    res.send({ user: req.name, age: req.age, email: req.email });
  }
);

http://localhost:3000/user returns {"user":"John Doe","age":42,"email":"jodo@test.com"}


routing

These routing methods specify a callback function (sometimes called “handler functions”) called when the application receives a request to the specified route (endpoint) and HTTP method. In other words, the application “listens” for requests that match the specified route(s) and method(s), and when it detects a match, it calls the specified callback function.

In fact, the routing methods can have more than one callback function as arguments. With multiple callback functions, it is important to provide next as an argument to the callback function and then call next() within the body of the function to hand off control to the next callback.

More middleware syntax examples can be found in the docs: