Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

JavaScript Express Basics Middleware Middleware Sequence and Routing

Trevor Maltbie
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
Trevor Maltbie
Full Stack JavaScript Techdegree Graduate 17,020 Points

"one two" prints twice each.

in my terminal (running macOS 10.13.6), I get One Two printed twice each like so

One
Two
One
Two

with this code:

app.use((req, res, next) => {
  console.log('One');
  next();
})
app.use((req, res, next) => {
  console.log('Two');
  next();
})

Any idea what is happening?

2 Answers

Hi trevor

Its hard to say without more information as this could be due to a lot of different causes.

Can you run your app again in debug mode with the following:

DEBUG=express:* node index.js

Assuming your app is index.js, if not change it appropriately

Thanks

It probably comes from that you get redirected as this will be another call. If you dont have a cookie and trying to access the index page the terminal will call one time for requesting the index page and then another time to redirect you to the hello page and therefor sending another request and another call to the terminal

Seth Foss
Seth Foss
7,097 Points

I observed the same thing during this lesson. I didn't have any redirects, but my browser (Firefox) is trying to request a favicon. Even though I don't have a favicon configured in this app, the request is being processed enough by node to trigger the middleware functions we are practicing in this lesson.

To confirm, I used the dev tools to "Resend" just the favicon request, and the middleware printed to the console once.