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


Kenneth Kim
Kenneth Kim
3,795 Points

Difference Between Express.Router() and express() variables

const express = require('express');
const app = express();
const router = express.Router();



In one of the questions in the quiz this was asked: "express.Router() creates an object that behaves similar to the app object."

The correct answer is 'True'. I know that we can both create routers by using either of the two but is it safe to say that they are not the same in all cases? If my understanding is correct, the express() variable can do more things like start a server while the other one cannot.

Can anyone provide a simple explanation so that I can understand when to use each of them? Thanks :)

(Please note the code above is not complete and are situated in several files. I just summarized how they are declared)

Harrison Cassedy
.a{fill-rule:evenodd;}techdegree seal-36
Harrison Cassedy
Java Web Development Techdegree Graduate 12,031 Points

I may be wrong here but this is what I'm seeing.

it looks like you are using a compound expression. This means that there is a certain order to what should and is going to go first. This indicates the data type, access level, and scope. express is using parenthesis to indicate which field is going to go first by saying you need to have express before.

so what your code is saying in human speak: "Your first line of code says your application requires the Express.js dependency, the second line of code tells your app.js to use Express.js. The GET request is sent by the Express.js router to the server to retrieve data."

if this helps please upvote :)

Kenneth Kim
Kenneth Kim
3,795 Points

Hi. The declarations is not in the proper format compared to what I have in my work space. I just did that to summarized :) Question was updated.

1 Answer

https://expressjs.com/en/guide/routing.html From the docs

express.Router Use the express.Router class to create modular, mountable route handlers. A Router instance is a complete middleware and routing system; for this reason, it is often referred to as a “mini-app”.

The following example creates a router as a module, loads a middleware function in it, defines some routes, and mounts the router module on a path in the main app.

Create a router file named birds.js in the app directory, with the following content:

var express = require('express')
var router = express.Router()

// middleware that is specific to this router
router.use(function timeLog (req, res, next) {
  console.log('Time: ', Date.now())
// define the home page route
router.get('/', function (req, res) {
  res.send('Birds home page')
// define the about route
router.get('/about', function (req, res) {
  res.send('About birds')

module.exports = router

Then, load the router module in the app:

var birds = require('./birds')

// ...
app.use('/birds', birds)

The app will now be able to handle requests to /birds and /birds/about, as well as call the timeLog middleware function that is specific to the route.


I am a newbie in the Tech Degree program. I need a point of clarification on your comment above. Can I assume the the code in the first block goes in a file named something like router.js and the second block goes in app.js. I am having routing problems on Project 10 in the Tech degree program. I may just go ahead and post my problem and give folks a link to my github repo.