如何从URL获取id作为变量?在Node.js中

Kar*_*sia 1 express

这是我在Node.js上运行的快速应用程序

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

app.get('/', function(req, res){
  res.send('id: ' + req.query.id);
});

app.listen(3000);
console.log('working...');

//so i need url id as var
var gotID = req.query.id

//want to use this data to another js file
module.exports = gotID;
Run Code Online (Sandbox Code Playgroud)

所以我想把这个URL id作为我的变量.

这看起来像这个URL

http://localhost:3000/[id that i want]
Run Code Online (Sandbox Code Playgroud)

所以我该怎么做?

我是Node.js的新手

rue*_*uel 8

这应该适合你:

app.get('/:id', function(req, res) {
    res.send('id: ' + req.params.id);
});
Run Code Online (Sandbox Code Playgroud)

req.query用于搜索查询参数(即一切后?的http://something.com/path?foo=var)