Route

  1. Route Concept

    Routing is a mapping relationship. In Express, routing refers to the mapping between client requests and server processing functions.

    The route in Express consists of three parts, which are request type, requested URL address, and processing function. The format is as follows:

    1
    app.METHOD(PATH, HANDLER)

    For example, a simple route:

    1
    2
    3
    4
    5
    6
    app.get('/', function(req, res){
    res.send('Hello World!')
    })
    app.post('/', function(req, res){
    res.send('Got a POST request')
    })

  1. Route matching process

    Whenever a request arrives at the server, needs to be matched by the route first, and only after the match is successful, the corresponding processing function will be called.

    When matching, it will be matched in the order of routing. If the request type and request URL match successfully at the same time, then Express will Transfer this request to the corresponding function for processing.

    The matching is performed according to the defined sequence. When the matching is successful, the following routes will not be matched again (that is, the preceding routes have higher priority). If the match is successful, the corresponding function will be called, and if the match is unsuccessful, the match will continue.


    For routes registered by methods such as app.get(), the path needs to be completely matched, and the request method must be completely matched to take effect.

    But app.use() is special, because express uses regular expressions to match (that is, the path of app.use() parameter 1 can be written as positive side expression, usually not necessary). In regular expressions * means match everything. and app.use() adds a * after the incoming string parameter, so as long as the path at the beginning of the incoming string is considered a successful match, it does not need to be completely match.

    We also use app.use(express.json()) when registering global routes, that is, the first parameter is not written. In fact, the first parameter here is * (app.use() added for the path itself), but it is omitted, and the result is that all paths can be matched.


  2. Simple usage of route

    The easiest way to use a route in Express is to mount the route to the app, such as:

    1
    2
    3
    4
    5
    6
    const express = require('express')
    const app = express()
    app.get('/', (req, res) => { res.send('Hello World!') })
    app.post('/', (req, res) => { res.send('Got a POST request') })

    app.listen(80, () => {console.log('server running at http://127.0.0.1')})


  1. Modularization of route

    In order to facilitate the modular management of routes, Express does not recommend mounting routes directly to the app, but recommends extracting routes into separate modules. Proceed as follows:

    1. Create a .js file corresponding to the routing module

    2. Call the express.Router() function to create a route instance object (in the route module file)

      1
      2
      const express = require('express')
      const router = express.Router()
    3. Mount the specific route to the routing instance object (in the routing module file)

      1
      2
      router.get('/user/list', (req, res) => { res.send('Get user list.') })
      router.post('/user/add', (req, res) => { res.send('Add new user.') })
    4. Use module.exports to share routing objects outward (in routing module file)

      1
      module.exports=router
    5. Register the routing module with the app.use() function (in the main file)

      1
      2
      const userRouter = require('./router/user.js') //Import routing module
      app.use(userRouter) //register route

      Note: The purpose of the app.use() function is to register the global middleware.


Share