(Sorry for the lengthy post, thanks for reading!)
I have two questions about this step. I’m going to refer to code in a prior step and my code in the current step so I’m just gonna define that at the top here. In the prior step I mounted (is that the right term?) a function to log the request method and path, and IP address. In the current step I mounted (?) a body parser.
Prior step:
app.use((req, res, next) => {
console.log(`${req.method} ${req.path} - ${req.ip}`);
next();
});
Current step:
app.use(bodyParser.urlencoded({extended: false}));
Question 1 - In the prior step we defined a middleware function that had a request, a response, and next
callback function parameters, and had to invoke next
at the bottom. In the current step we don’t have any of those parameters. Why? This applies to express.static() too, that confused me also since it’s kinda the same thing happening. What’s the key difference that I don’t understand?
Question 2 - In the current step I just replaced the code for my prior step and passed the tests, but what if I wanted to use both, how would that look? I tried both of these methods below and both times my app seemed to run without any errors or antyhing, and both worked when trying to access the body data in the step 12 that comes after this, so are both fine?
Option 1:
app.use((req, res, next) => {
console.log(`${req.method} ${req.path} - ${req.ip}`);
next();
}, bodyParser.urlencoded({extended: false}));
Option 2:
app.use((req, res, next) => {
console.log(`${req.method} ${req.path} - ${req.ip}`);
next();
});
app.use(bodyParser.urlencoded({extended: false}));
To summarize, I’m just kinda confused about middleware, lol.
Lesson:
Gitpod Snapshot:
https://gitpod.io#snapshot/949bd5b3-267c-4796-b78e-34ae5474089f