FAQ: Learn Express Routes - Getting A Single Expression

This community-built FAQ covers the “Getting A Single Expression” exercise from the lesson “Learn Express Routes”.

Paths and Courses
This exercise can be found in the following Codecademy content:

Learn Express

FAQs on the exercise Getting A Single Expression

Join the Discussion. Help a fellow learner on their journey.

Ask or answer a question about this exercise by clicking reply (reply) below!

Agree with a comment or answer? Like (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!

I don’t really understand what this Express Yourself machine does.

By clicking “REFRESH EXPRESSIONS” button, what kind of HTTP request is sent?

Unless we know exactly what this app does, following the series of instructions in this unit won’t be educational enough for us to understand how Express.js works…

11 Likes

In this exercise, the const express is declared by requiring from the express module, no brackets. For the getElementById and seedElements, why are they declared with brackets { } ?

In this exercise, the const express is declared by requiring from the express module, no brackets. For the getElementById and seedElements, why are they declared with brackets { } ?

this one is too fragile, I’ve just added console.log(foundExpression);

app.get('/expressions/:id', (req, res, next) => {
  const foundExpression = getElementById(req.params.id, expressions);
  console.log(foundExpression);
  res.send(foundExpression);
});

it breaks immediately :roll_eyes:

I believe, unless I am missing something, there is a cleaner way to complete that task:

app.get('/expressions/:id', (req, res, next) => {
  const newIndex = Number(req.params.id) - 1
  res.send(expressions[newIndex])
});

I am completly lost in this excercise, the notation for declaring const { getElementById, seedElements } and params key word, just come out of the blue and don’t make sense of what I learned so far.
By reading the coments here, I kind of get that the const declaration is a way to import in express

12 Likes

In this exercise, how does declaring this specify the name of hydra?

console.log(req.params) // { name: 'hydra' };
5 Likes

Instead of writing these two helper functions twice like this:

const getElementById = require('./utils');
const seedElements = require('./utils');

You can wrap them up in a flower bracket with a comma separated as both functions are imported from the same utils folder.

const { getElementById, seedElements } = require('./utils');

Hope this helps,
Cheers :slightly_smiling_face:

3 Likes

I did that…

app.get('/expressions/:id', (req, res, next) => {
  res.send(getElementById(req.params.id, expressions));
});

…but it failed for a while. I’ve just forgotten to refresh the page.

1 Like

There needs to be some more guidance provided to understand what this exercise is trying to achieve. getElementById is JS pointing to item on a webpage - this is not how many APIs are developed and work - and is an added complication when trying to explain that GET methods can call multiple JSON objects or a single JSON object.

5 Likes

I have to agree with my predecessors that this lesson is overcomplicated and some more guidance should have been given in the beginning about what the express yourself machine is and how it works.

To shed some light on the issues many of us are having here:

const { getElementById, seedElements } = require('./utils');

This line of code is added to make the “express yourself machine” actually work and populates the “expressions” array with 3 emojis. As already explained by @cloud5675760806, it is ES6 syntax for importing two functions with one line of code from the utils folder.

After you have implemented the app.get( ) route for the "/expressions/:id" path, you should start your server in the terminal with node app.js , refresh the webpage on the right hand side so that the express yourself machine is shown, and at the top left of the webpage select GET to test your implemented route. You have to enter an id between 1 and 3 and if implemented correctly, an emoji and its name should be shown and the status code 200 should appear.

@callmej9 It only specifies the name of hydra if you invoke the get request with the path expressions/hydra in this case.
As an example, you can add console.log(req.params) to the function of your app.get() route like this:

app.get("/expressions/:id", (req, res, next) => {
  console.log(req.params);
  res.send(getElementById(req.params.id, expressions));
})

This will print the params object for every get request that you perform with the express yourself machine to the terminal and you can observe how it changes.

4 Likes

@codecademy I know that this forum is most of all meant to discuss and find solutions among the users, but please forward this lesson to your content team to revise and improve the explanations.

5 Likes

const monsters = { hydra: { height: 3, age: 4 }, dragon: { height: 200, age: 350 } };
// GET /monsters/hydra
app.get(’/monsters/:name’, (req, res, next) => {
console.log(req.params) // { name: ‘hydra’ };
res.send(monsters[req.params.name]);
});

In the above snippet, is the monsters object missing a ‘name’ property, that is, should ‘hydra’ and ‘dragon’ be values to a name key in their respective objects? Until this point, i haven’t seen any examples of a route matching without a property of the same name. When the text describes wildcards, " For example /monsters/:id will match both /monsters/1 and /monsters/45", I assume 1 and 45 are the values to ‘id’ keys. The Express documentation appears to confirm my thoughts about the wildcard/key paring, but I could be interpreting it incorrectly.

Because you specified your route param as :name '/monsters/:name', when a request is sent with the lets’s call it “argument” hydra, or whatever string '/monsters/hydra' or '/monsters/dalek', said “argument” will be matched with the key :name in your params object.

Then you can access in the monsters object the property with the name of your argument (hydra or whatever), by using the alias req.params.name where the argument is stored.

At least that’s how I interpreted it. It really is kind of confusing and poorly explained, I hope my rambling about it helped.

2 Likes

Hey, thanks for replying. I went through the lessons a second time and poked around a bit elsewhere and reached the same conclusion you’ve presented.

Cheers

1 Like

thanks for the explanation
the truth is that these exercises leave a lot of things unexplained. (at least for me)

Thing is, this weren’t explained. It just popped up in the code with no explanation

2 Likes

I have a good one. Why on earth is :

app.use(express.static(‘public’));

being used twice in one file ??? Lesson 4 & 6 so far i’ve seen. I have never seen this before, and surely if it was a bug somebody would have spotted it before now

?

1 Like

// Use static server to serve the Express Yourself Website
app.use(express.static(‘public’));

I think it’s re-loading the webpage each time ‘app.use’ is called.