Luud van Keulen
Luud van Keulen

Reputation: 1254

Difference between app.use('/', router) and app.use(router)

I am working on a node.js application and saw that some people use
app.use('/',router)
and some people use
app.use(router)
What is the difference between these two and which one should I use?

Upvotes: 0

Views: 91

Answers (1)

Roman Yankovsky
Roman Yankovsky

Reputation: 1217

There's no difference in this case.

app.use([path,] callback [, callback...])

If you call app.use without specifying path explicitly, it takes a default value which is /.

But using this paramaeter you can have more than a single router in your app. See examples in the documentation:

var express = require('express');

var app = express(); // the main app
var admin = express(); // the sub app

admin.get('/', function (req, res) {
  console.log(admin.mountpath); // /admin
  res.send('Admin Homepage');
});

app.use('/admin', admin); // mount the sub app

Upvotes: 2

Related Questions