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

**URL:** <https://forum.freecodecamp.org/t/how-does-this-work-where-can-i-read-more-to-understand-better-how-do-you-debug/448366>\
**Category:** Curriculum Help\
**Tags:** javascript\
**Created:** [February 25, 2021, 12:13pm UTC](https://forum.freecodecamp.org/t/how-does-this-work-where-can-i-read-more-to-understand-better-how-do-you-debug/448366 "2021-02-25T12:13:05Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![mseibert](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/mseibert/32/169303_2.png) [@mseibert](https://forum.freecodecamp.org/u/mseibert)\
**Post date:** [February 25, 2021, 12:13pm UTC](https://forum.freecodecamp.org/t/how-does-this-work-where-can-i-read-more-to-understand-better-how-do-you-debug/448366/1 "2021-02-25T12:13:06Z")

</div>

**Tell us what’s happening:**

I think I have basically understood what [the general task is here](https://www.freecodecamp.org/learn/apis-and-microservices/basic-node-and-express/implement-a-root-level-request-logger-middleware).

I have a couple of general questions:

- This code comes out of nowhere that I got from github into repl.it. 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 repl.it 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”:

```auto
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:
  next();
});
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:**

> **[freeCodeCamp.org](https://www.freecodecamp.org/learn/back-end-development-and-apis/basic-node-and-express/implement-a-root-level-request-logger-middleware)**
>
> Learn to Code — For Free

---

<div class="post-metadata">

**Author:** ![Marmiz](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/marmiz/32/74845_2.png) [@Marmiz](https://forum.freecodecamp.org/u/Marmiz)\
**Post date:** [February 25, 2021, 2:34pm UTC](https://forum.freecodecamp.org/t/how-does-this-work-where-can-i-read-more-to-understand-better-how-do-you-debug/448366/2 "2021-02-25T14:34:14Z")

</div>

> [@mseibert](#):
>
> This code comes out of nowhere that I got from github into repl.it. 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?

Repl.it 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.

> [@mseibert](#):
>
> I have trouble debugging and trying things out in the repl.it 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?

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.

> [@mseibert](#):
>
> 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

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 ✨

---

<div class="post-metadata">

**Author:** ![mseibert](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/mseibert/32/169303_2.png) [@mseibert](https://forum.freecodecamp.org/u/mseibert)\
**Post date:** [February 25, 2021, 6:18pm UTC](https://forum.freecodecamp.org/t/how-does-this-work-where-can-i-read-more-to-understand-better-how-do-you-debug/448366/3 "2021-02-25T18:18:20Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![system](https://global.discourse-cdn.com/freecodecamp/original/3X/e/1/e1c433102e34b46dfaa3e15d13ab9a514b127078.svg) [@system](https://forum.freecodecamp.org/u/system)\
**Post date:** [August 27, 2021, 6:18am UTC](https://forum.freecodecamp.org/t/how-does-this-work-where-can-i-read-more-to-understand-better-how-do-you-debug/448366/4 "2021-08-27T06:18:48Z")

</div>

Closed on August 27, 2021, 6:18am UTC

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