How does this work? Where can I read more to understand better? How do you debug?

Tell us what’s happening:

I think I have basically understood what the general task is here.

I have a couple of general questions:

  • This code comes out of nowhere that I got from github into Am I right to assume, that this is an intended abstraction to make things easier for me? So I simply do not care at this point? Or did I miss that part because I only did the courses “Responsive Web Design” and “JavaScript Algorithms and Data Structures” so far and skipped “Front End Development Libraries” and “Data Visualization”. I would not have gotten to know that in these courses, right?
  • I have trouble debugging and trying things out in the environment. In the “JavaScript Algorithms and Data Structures” - part of the curriculum you could see the console in real time to react on your code. Now you have to run the server and dig through the shipload of messages that are generated, right?
  • In the code below this console complains

“var string = req.method + " " + req.path + " - " + req.ip;
ReferenceError: req is not defined”

I was under the impression that req.method, req.path, and req.ip would be variables and methods that were defined elsewhere in the magical black box, I am using here.

I seem to be missing a basic understanding of the whole concept. Is there more for me to read? Can you help me with your answers?

Your code so far

The code in the file “myApp.js”:

var express = require('express');
var app = express();
var string = req.method + " " + req.path + " - " + req.ip;

app.use(function middleware(string) {
  // Do something
  // Call the next function in line:
app.use("/public", express.static(__dirname + "/public"));

Your browser information:

User Agent is: Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/88.0.4324.150 Safari/537.36.

Challenge: Implement a Root-Level Request Logger Middleware

Link to the challenge: is there to make things easier both for you, as you don’t have to set up all the local environment by yourself, as well as FCC / anyone you share your code with.

It’s especially important the latter.
You are now developing a simple api application, meaning you’ve entered the realm of back end development.
By the very nature of it each one of us can develop using a different OS, different node versions and package versions.
Same as before the browser was taking away all of this complexity, now Repl is taking it away.

You can still add console.log everywhere and look at Repl console. Of course each time you change anything in your source code you need to run it again to see its effects.

There’s no “magic”. All that the Repl is doing is creating a node development environment and adding ExpressJS as dependency. That’s all.
All the rest is up to you and what you add in your source code.

In this case you are exploring Express middlewares.
Reading the challenge description that’s the definition provided:

Middleware functions are functions that take 3 arguments: the request object, the response object, and the next function in the application’s request-response cycle

The error is arising since you are trying to use req in the “global” scope of your JS file. Request and Response are available inside an Express’ Middleware.
You should try to use them there, as they are undefined everywhere else.

Hope it helps :sparkles:

1 Like

Thanks. I understand the part with the variable scope bow. Thanks for your help.