FAQ: Learn Express Routes - Writing Your First Route

This community-built FAQ covers the “Writing Your First Route” 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 Writing Your First Route

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!

Hello! I am not getting any feedback after clicking “Check Work”. Is it not supposed to show a pop-up window of sorts? I am currently “stuck” on “3. Writing Your First Route”. I can’t tell if the code I have written is 100% correct. Help please? Also, even though the value 4001 is specified, terminal still says Listening to port 4005. Why?

const express = require('express');
const app = express();

const PORT = process.env.PORT || 4001;
// Use static server to serve the Express Yourself Website

// Open a call to `app.get()` below:
app.get('/expressions', (req, res) => {
} );

app.listen(PORT, () => {
  console.log(`Listening on port ${PORT}`);

Hello betauser76, you might have already fixed this by now but first if you take a look at PORT, it has a condition, it will either equal whatever value is stored in PORT from the environment variables (which in your case was true and turned out to be 4005) or it will assign it the number 4001.

also according to the steps, you’re required to console.log the request in the callback function, where you are right now just console logging:

The main thing is that once you start the server and click on check work, it should only give you a check mark on the step if you correctly completed the task, it will only show a pop up if there was an error.

1 Like

I was lost with the arguments of the function created so I looked for it, this is the best response I found, this is from https://flaviocopes.com/express/

There is a method for every HTTP verb : get() , post() , put() , delete() , patch() :

app.get('/', (req, res) => { /* */ })
app.post('/', (req, res) => { /* */ })
app.put('/', (req, res) => { /* */ })
app.delete('/', (req, res) => { /* */ })
app.patch('/', (req, res) => { /* */ })

Those methods accept a callback function, which is called when a request is started, and we need to handle it.

We pass in an arrow function:

(req, res) => res.send('Hello World!')

Express sends us two objects in this callback, which we called req and res , that represent the Request and the Response objects.

This has always baffled me and I’ve always felt too dumb to ask. How do we tell our code what our base URL is? For example, in our demonstrations we’re listening on port 4001, and we’re making routes for get/monsters. No where do we tell the code that its mywebsite.com/monsters vs yourwebsite.com/monsters.

I thought it would be console.log(req);



Inside app.js , create a route handler to handle a GET request to '/expressions' . For now, give it a req, res, next callback. For now, log the req object inside the callback.

I guess

const express = require('express');
const app = express();
const PORT = process.env.PORT || 4001;
app.get('/expressions', (req, res, next) => {

app.listen(PORT, () => {
  console.log(`Listening on port ${PORT}`);

node app.js
give’s me an error, so how do I log the req object inside the callback.?

1 Like

Hi, this is far down the line and I hope you sorted it but for those who aren’t getting a response in the terminal this might be useful;

if you have typed node app.js in the terminal before writing your app.get() method you will need to control + c out of the terminal and then enter node app.js again and you will see your response.

hope this helps

1 Like

What is this next ? how is it used ?

I also went for console.log(req), but was getting an error. This message is what fixed it for me i.e. control + c and restart your program.

Interesting that the solution does not include console.log(req), but console.log(req) is correct when “check work” regardless.

Hello I’m currently doing the “Learn Express Routes” lesson and I’m stuck at lesson 3.
I have this lesson working on the codeacademy site but when I load it into my computer and run it through node the HTML is launched but the Refresh Expression Button is inactive.
The app.get statement in app.js does produce the object array and I can see it in localhost:4001/expressions but localhost:4001 just shows a static image with all buttons being inactive.
This is not the case in cadeacademy where all the buttons are active.
I’ve installed the necessary JSON files as well as express and I’ve checked all my code and it is the same as in codeacademy.

The variable names ‘req’ and ‘res’ are too similar; the first two letters are the same. For me that makes it hard at a quick look to distinguish them. I assume they are abbreviations of request and result. I hope to change one of them. Any ideas for an alternative 3-letter variable name, for one or other?

Why is the ‘req’ object also logged when I only call “localhost:4001/”? I thought it would only be logged when we call the ‘/expressions’ route? I also observed this behavior in another project from another website…