What is the difference between get / post / ... and using

All:

I am new to Express 4 router.

When I tried some login / registration example, I got a question about the .use and .get / .post function:

I have seen that sometimes it uses:

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

router.get('/hello', function(req, res, next) {
  res.send("Welcome");
});

      

and in the main application we use it like:

app.use("/", router);

      

Other times he uses:

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

//here the router uses .use() function rather than .get/.post
router.use('/hello', function(req, res, next) {
  res.send("Welcome");
});

      

and in the main application we use it like:

app.use("/", router);

      

So I'm wondering what is the difference between the two, is using .use () just a common name for all get / post / put / ... together?

I find this post: Difference between app.use and app.get in express.js

But still it is not easy to understand it ....

thank

+3


source to share


2 answers


In addition to what Jonathan Lonowski said in the posted link, it might help not to compare use

with get

and post

, but rather compare it with all

, since and all

, and use

regardless of the HTTP verb used, while this is clearly not true for get

. Everything I'm going to say applies, if you replace "everything" with "get" it will just reduce that handler to a specific HTTP verb.

So what's the difference between all

and use

?

app.all will handle incoming requests on the specified URL path regardless of the HTTP verb, as it does app.use

. However, the way it compares the requested url to the handler is different. For example:

var r = express.Router();
r.use('/foo', function (...) { ... }); // Route A
r.all('/bar', function (...) { ... }); // Route B

      

If you complete the request /foo/123

Route A will be .

If you make a request, /bar/123

Route B will NOT run .

This is because the full path is expressed in the HTTP expression, but with "use" it only cares about the beginning of the URL. Since the URL /foo/123

starts with /foo

Route A will run, but since it /bar/123

does not match a FULL address, there will be no route B. Note. You can make the behavior .all

the same way: r.all('/bar/*', ...)

but use

simpler and more suitable for this.

So, what you would like to set with one against the other is different. For example:



var app = express();
var router1 = express.Router();
var router2 = express.Router();

router2.all('*', function (req, res) { ... }); // Must specify a path!

router1.use('/secondary-routes', router2); // Can't do this with all.

app.use(router1); // Look Ma, no path!

      

Here I used all

to handle the incoming request, where I used use

to mount the entire router. Also note that using the functions router.METHOD

requires the URL string as the first parameter, use

not.

At the end of the day, if you:

  • It is required that all requests that come in the given path (or even every request) use the specified middleware, or
  • Want to connect all additional router / app or
  • Want to include a plugin in your application.

... Then use

you probably want to.

If you:

  • Process a specific request on a specific URL path (i.e., maybe don't match * in the URL)
  • Usually will not call next

    and will handle the request instead

... Then the HTTP verb method (like get

, post

or all

) is probably what you want.

+3


source


.use

used in two cases: middlewares and "modular mountable route handlers".

In your example

router.use('/hello', function(req, res, next) {
  res.send("Welcome");
});

      



This means that any requests sent to /hello

will be completed with a "Welcome" and the actual .get

one attached to /hello

will not be called.

So, in short, call use

when you need to apply some common intermediaries or want to do a modular architecture with routers. use

can be "used" as request handlers, but you should not, because it is not intended for this purpose

+1


source







All Articles