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 Build a REST API With Express Building API Routes in Express Using Middleware in Express

Kurt Bitner
Kurt Bitner
12,135 Points

Middleware not executing

My code is exactly like the instructor's but when I make a GET request in either Postman or firefox none of the middleware logs to the console.

This is my code:

'use strict';

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

app.use(function(req, res, next){ console.log("First Middleware!"); return next(); });

app.use(function(req, res, next){ console.log("Second Middleware!"); return next(); });

var port = process.env.PORT || 3000;

app.listen(port, function(){ console.log("Server running!"); });

1 Answer

Hey dude,

I don't think the instructor returns the next function. I believe he just executes it.

var express = require('express'); 
var app = express();
var port = process.env.PORT || 3000;

app.use((req, res, next) => { 
  console.log("First Middleware!");  
  next(); <-- HERE
});

app.listen(port, () => { 
  console.log("Magic is happening!"); 
});

Hope this helps.

:-)