This community-built FAQ covers the “Setting Status Codes” exercise from the lesson “Learn Express Routes”.
Paths and Courses
This exercise can be found in the following Codecademy content:
Web Development
Learn Express
FAQs on the exercise Setting Status Codes
Join the Discussion. Help a fellow learner on their journey.
Ask or answer a question about this exercise by clicking reply (
) below!
Agree with a comment or answer? Like (
) to up-vote the contribution!
Need broader help or resources? Head here.
Looking for motivation to keep learning? Join our wider discussions.
Learn more about how to use this guide.
Found a bug? Report it!
Have a question about your account or billing? Reach out to our customer support team!
None of the above? Find out where to ask other questions here!
Hi, I tried to use this code in my Atom text editor:
var express = require(‘express’);
var app = express();
const monsterStoreInventory = { fenrirs: 4, banshees: 1, jerseyDevils: 4, krakens: 3 };
app.get(’/monsters-inventory/:name’, (req, res, next) => {
const monsterInventory = monsterStoreInventory[req.params.name];
if (monsterInventory) {
res.send(monsterInventory);
} else {
res.status(404).send(‘Monster not found’);
}
});
app.listen(3000)
However, when I try to access the following path in my browser http://localhost:3000/monsters-inventory/jerseyDevils, I receive the following error:
RangeError [ERR_HTTP_INVALID_STATUS_CODE]: Invalid status code: 4
at ServerResponse.writeHead (_http_server.js:208:11)
at ServerResponse._implicitHeader (_http_server.js:199:8)
at ServerResponse.end (_http_outgoing.js:712:10)
at ServerResponse.send (/Users/viktorkarpanasiuk/Programming/ninjaProject/node_modules/express/lib/response.js:221:10)
at app.get (/Users/viktorkarpanasiuk/Programming/ninjaProject/app.js:9:9)
at Layer.handle [as handle_request] (/Users/viktorkarpanasiuk/Programming/ninjaProject/node_modules/express/lib/router/layer.js:95:5)
at next (/Users/viktorkarpanasiuk/Programming/ninjaProject/node_modules/express/lib/router/route.js:137:13)
at Route.dispatch (/Users/viktorkarpanasiuk/Programming/ninjaProject/node_modules/express/lib/router/route.js:112:3)
at Layer.handle [as handle_request] (/Users/viktorkarpanasiuk/Programming/ninjaProject/node_modules/express/lib/router/layer.js:95:5)
at /Users/viktorkarpanasiuk/Programming/ninjaProject/node_modules/express/lib/router/index.js:281:22
When I try to access the monster, the is not defined, I receive ‘Monster not found’. This shows that the code works, however, I still have the problem with accessing the monsters
Hi,
I also tried and got the same error. Hope I understand why:
The value sent is a number and we must send string instead of number.
So I changed the example code
from res.send(monsterInventory);
(the type of this data is Number)
to res.send(`${monsterInventory}`);
(the type of this data is String).
Now works as expected…
1 Like
Why is the exercise wrong if I do only:
res.status(404)
instead of:
res.status(404).send()
What’s the difference?
In the first instance you’re not appending it to send()
so no response is actually being given.
You can also do this:
res.status(404);
res.send("x");
This will work because you’re specifying a response.