这是我在 Node.js 上运行的 Express 应用程序
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;
所以我想要那个 URL id 作为我的变量。
看起来像这个网址
http://localhost:3000/[id that i want]
所以我该怎么做?
我是 Node.js 的新手
最佳答案
这应该适合你:
app.get('/:id', function(req, res) {
res.send('id: ' + req.params.id);
});
req.query 用于搜索查询参数(即
?
之后的所有内容 http://something.com/path?foo=var
)
关于express - 如何从 URL 获取 id 作为变量?在 Node.js 中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42033209/