Are Router() and express() methods interchangeable?

Question

Are Router() and express() methods interchangeable?

Answer

I see how they could appear to be so, we commonly use express() defined as app, ie.:

const express = require('express');

const app = express();

and from our same express variable (object) we are getting Router, ie.:

const myNewRoute = express.Router();

Which allows them to perform similar functions for requests and responses, ie.:

app.get('/', (req, res) => console.log(res));
                  /*VS*/
myNewRoute.get('/', (req, res) => console.log(res));

But that is where the line is drawn, both come from the express function, and both perform get, put, post, create, and delete requests.

The main difference is that express() is a top level function, which means it performs core functionality for the library and it contains its own methods where, as a matter of fact, Router is one, and that is why when we create a specific router we chain the Router() method on express, kind of like how we use app.use().

express() handles server connection, routes, middleware, implementation of pages (HTML && CSS to be directed to a route) and communication with databases (like SQL) while our routers only handle routes just like a person on the street would give a tourist directions to where they want to go.

Now, there is a reason why we do not say app.Router() and that is because app becomes the application server if we used app.Router() it would be handling the same routes across the board instead of separating concerns when we write ... = express.Router(); we create like a pocket application, this small version of app that specializes in routes will only know about anything coming to the main path that is assigned in app.use() and that is why it makes it a very helpful practice to separate it, since it will also provide us with an easier way to debug.

4 Likes