learning express step(七)

Route handlers enable you to define multiple routes for a path.

The example below defines two routes for GET requests to the /user/:id path.

The second route will not cause any problems, but it will never get called because the first route ends the request-response cycle.

const express  = require('express');
const app =  express();

app.get('/user/:id', function (req, res, next) {
    console.log('ID:', req.params.id);
    next();
},function (req, res, next) {
    res.send('User Info');
});

app.get('/user/:id',function (req, res, next) {
    console.log(req.params.id);
    res.end(req.params.id);
    //res.send(req.params.id);
});

app.listen(3000);

result:

C:UsersadminWebstormProjectslearning-express-step7>node  learning-express-step7.js
ID: id=123
ID: id=123

web result:

原文地址:https://www.cnblogs.com/lianghong881018/p/11011133.html