FAQ: Code Challenges - Code Challenge

This community-built FAQ covers the “Code Challenge” exercise from the lesson “Code Challenges”.

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

Web Development

Learn Express

FAQs on the exercise Code Challenge

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!

Could someone please explain why app.use(cors()) (in the previous exercise) requires the “()” after cors whereas app.use(’/data/:index’, indexExists) doesn’t use “()” like: “…indexExists())”

I’m sure this is basic and I’ve probably read about it plenty of times but my memory is sieve-like!

Thanks!

Todd

Not sure if I understand your question. However, () as in parentheses is used with all functions whether they receive parameters or not.
Functions - MDN

Thanks for your reply Scott!

I’m still unsure as to why CORS middleware package is passed to app.use(…) with “()”, as in: app.use(cors());

In the previous exercises middleware functions are passed like regular callback functions, with no “()”.

Examples being:

  • app.use(moodleware);
  • app.use(timeMiddleware);
  • app.use(errorHandler);

I understand (vaguely) that this is because they are referenced here rather than being invoked immediately. I thought that this would also be the case when passing “cors”, but I’m very much a noob and am frequently caught out by these simple things.

BASICALLY…Why can’t it be:

  • app.use(cors);

like

  • app.use(moodleware);

???

Todd

Using middleware
After you read that read the next link
cors

If that is the case then yes you would omit the () to reference it in with something like console.log. Perhaps you would want to do that to make sure you are returning the expected result before you move on and return some kind of value/s. You can test that out with other more simple things in the console.