最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Express, diff between route.use() ,route.all(),route.route() - Stack Overflow

programmeradmin2浏览0评论

If router.all() just match all methods,could it be instead by router.use()? and what router.use() diff between router.route()?

If router.all() just match all methods,could it be instead by router.use()? and what router.use() diff between router.route()?

Share Improve this question asked Aug 30, 2017 at 2:55 sinbarsinbar 1,0632 gold badges9 silver badges26 bronze badges
Add a comment  | 

4 Answers 4

Reset to default 10

router.all: What this means is, it doesn't matter the method of the request.. (post, get, put), if the url matches, execute the function.

ex- router.all("/abc",fn) will be work for all request to /abc

router.use() : router.use() helps you write modular routes and modules.. You basically define a middle ware for routes.

router.use("/pqr", pqrRoutes)

now for all requests that start with /pqr like /pqr/new or /pqr/xyz can be handles inside the pqrRoutes.

router.route(): this is nice way to define the different Method implementations for a single url end point.

lets just say you have two api end points. router.get("/jkl") and router.post("/jkl"), with router.route() you cam sort of combine these different api handlers..

you can say router.route("/jkl").get(fn1).post(fn2)

router.all() matches every http protocol, router.use() is for middleware, and router.route() returns an instance of a single route which you can then use to handle HTTP verbs with optional middleware. You should check out the documentation for more informations

app.all(), which is not derived from any HTTP method. This method is used for loading middleware functions at a path for all request methods.

app.all('/secret', function (req, res, next) {
  console.log('Accessing the secret section ...')
  next() // pass control to the next handler
})

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.

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())
  next()
})

Bascially we use .use when we use a middleware 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. 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())
  next()
})
// define the home page route
router.get('/', function (req, res) {
  res.send('Birds home page')
})

There can we more than 1 router this instance of router has name router defined below the express defination. Here is the documentation for router https://expressjs.com/en/guide/routing.html

Router.use()

Routers behave like middleware and can be .use()d by the app in other routers. In other words, routers allow you to chunk your big app into numerous mini-apps that you can later put together. For small apps, this might be overkill, but as soon as you think, “This app.js file is getting big,” it’s time to think about breaking down your app with routers.

router.route(path)

The router.route(path) method is used to chain HTTP verb methods. For example, in a create, read, update, and delete (CRUD) server that has POST, GET, PUT, and DELETE endpoints for the /posts/:id URL (e.g., /posts/53fb401 dc96c1caa7b78bbdb), we can use the Router class as follows:

var express = require('express');
var router = express.Router();
router.param('postId', function(request, response, next) {
// Find post by ID
// Save post to request
request.post = {
 name: 'Node.js',
 url: 'http://your-url/node-js-blog'
   };
return next();
});

The Router.route(path) method provides the convenience of chaining methods, which is a more appealing way to structure, your code than re-typing router for each route. Alternatively, we can use router.VERB(path, [callback...], callback) to define the routes just as we would use app.VERB(). Similarly, the router.use() and router.param() methods work the same as app.use() and app.param().

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论