在nodejs中使用路径变量,在nodejs中使用查询值

moh*_*han 0 node.js express

我正在使用node.js中的express框架.我想访问路径变量和查询参数.当我只使用路径变量它工作正常,但使用查询参数它将无法工作建议我在单个API中使用正确的解决方案.在这里我提到代码.

app.get('/user/:id', function (req, res, next) {
    var url_parts = url.parse(req.url, true);
    var type = url_parts.params.id;
    Console.log('ID:', type);
    next();
}, function (req, res, next) {
    res.send('User Info');
});
Run Code Online (Sandbox Code Playgroud)

VG_*_*G__ 6

而不是使用var url.parse()你可以直接传递值req

req.params.id
req.query.queu
Run Code Online (Sandbox Code Playgroud)

例如:

var type = req.params.id;
var query = req.query.q;
Run Code Online (Sandbox Code Playgroud)