Middleware exercize 4/15

Why isn’t 'A sorcerer approaches!" being logged before each request?

app.use((req, res, next) => {
console.log(“A sorcerer approaches!”);
next();
});

app.get(‘/magic/:spellname’, (req, res, next) => {
console.log(“The sorcerer is casting a spell!”);
next();
});

app.get(‘/magic/:spellname’, (req, res, next) => {
console.log(The sorcerer has cast ${req.params.spellname});
res.status(200).send();
});

app.get(‘/magic/:spellname’, (req, res, next) => {
console.log(“The sorcerer is leaving!”);
});

// Accessing http://localhost:4001/magic/fireball
// Console Output:
// “A sorcerer approaches!”
// “The sorcerer is casting a spell!”
// “The sorcerer has cast fireball”

I expected:
// Console Output:
// “A sorcerer approaches!”
// “The sorcerer is casting a spell!”
// “A sorcerer approaches!”
// “The sorcerer has cast fireball”
//“The sorcerer is leaving”

It’s because only one request is actually sent to the server. It looks sort of like 3 requests are sent, but because the GET routes have the same path, the first one passes itself onto the second one, and so on. So one server request == one console.log("A sorcerer approaches!")

Thanks, super helpful!

1 Like

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