Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

express.Router() vs express() in express

As mentioned in express routing guide and this answer, we can create "mini-app" and use it from the main app. However I saw a code where it uses app instead of router in the module

app.js

var express = require('express');
var userRoutes = require('./routes/user');

var app = express();

app.use('/user', userRoutes);

module.exports = app;

routes/user.js

var express = require('express');
var app = express(); // not express.Router() !!

app.get('/:name', function(req, res) {
  var userName = req.params.name;
  res.render('user.jade', {
    userName: userName
  });
});

module.exports = app;

I assumed the correct usage in routes/user.js should be

router = express.Router()

instead of

app = express()

but app = express() also works! what are the differences and why router = express.Router() is better?

like image 347
Afriza N. Arief Avatar asked Feb 15 '19 11:02

Afriza N. Arief


People also ask

Is Express used for routing?

Express uses path-to-regexp for matching the route paths; see the path-to-regexp documentation for all the possibilities in defining route paths. Express Route Tester is a handy tool for testing basic Express routes, although it does not support pattern matching. Query strings are not part of the route path.

What is Express () in Express js?

Express is a node js web application framework that provides broad features for building web and mobile applications. It is used to build a single page, multipage, and hybrid web application. It's a layer built on the top of the Node js that helps manage servers and routes.

Is Express router a middleware?

Express is a routing and middleware web framework that has minimal functionality of its own: An Express application is essentially a series of middleware function calls.

What does Express router () do?

The express. Router() function is used to create a new router object. This function is used when you want to create a new router object in your program to handle requests.


1 Answers

When you are working with a server where there are many routes, it can be confusing to leave them in a Main file together. The let router = express.Router() option works differently than let app = express().

While the app returns an app object, router will return a small app fragment, similar to the app, where you will use logic to call them later on the Main.

The most important, about your question, is that a router, which is isolated, will not interfere with others in the application, being a single environment.

https://expressjs.com/en/api.html#router

A router object is an isolated instance of middleware and routes. You can think of it as a “mini-application,” capable only of performing middleware and routing functions. Every Express application has a built-in app router.

A router behaves like middleware itself, so you can use it as an argument to app.use() or as the argument to another router’s use() method.

like image 114
Arthur Ferreira Avatar answered Oct 09 '22 17:10

Arthur Ferreira