2017-02-03 162 views
0

这是Node.js的如何从URL中获取id作为变量?在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;

运行我的快递应用程序,所以我想这个URL id作为我的变量。

这是这个样子URL

http://localhost:3000/[id that i want]

那么,应该怎么办?

我是新来的Node.js加载

回答

4

这应该为你工作:

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

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

+0

好吗谢谢 :) –