Implement a Root-Level Request

anyone can tell me whats wrong with my code? Im in this section

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

literal doesnt work either

app.use((req, res, next)=> {
  console.log(`${req.method} ${req.path} - ${req.ip}`);

my Repl
tyvm <3

Hello there,

There is this note on the lesson:

Note: Express evaluates functions in the order they appear in the code. This is true for middleware too. If you want it to work for all the routes, it should be mounted before them.

Hope that makes sense

no that doesnt really help, what should I focus on in my code using that hint?
is there anything that shouldnt be or my code line should be placed elsewhere? :sweat_smile:
node is a bit confusing for me

Yes. In order for routes to be affected by middleware, the middleware must be mounted (use) before the routes are declared.

ok makes sense haha , I was thinking that it would be like javascript where if you hadnt got the req/res yet how can you know what it is? so it would seem kinda undefined/not existing yet
thx :slight_smile: It works

Glad you got it. Just to add:

  • So, far, this middleware is just defined as a callback function taking in a req, res. So, it is more similar to defining a function before using it.

The workflow for express is:

  • Declare app
  • Add middleware/static files, and define the app’s properties
  • Define the routes which may make use of the middleware

tyvm this helps a lot!! :smile: :smile:

1 Like