FAQ: Learn Express Routes - Writing Your First Route

#1

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!

#2

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
app.use(express.static('public'));

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

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

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.

#4

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.