Node.js:读取URL中传递的参数

时间:2011-09-13 07:27:35

标签: javascript node.js coffeescript

在rails中我向我的服务器发出POST请求:

response = Typhoeus::Request.post("http://url.localtunnel.com/request?from=ola&to=ole")
result = JSON.parse(response.body)

在Node.js应用中,我想阅读FromTo

app.post '/request', (req,res) ->
    console.log "I have received a request, sweet!!"
    sys.log req.params.from
    #sys.log "From: " + req.from + ", To: " + req.to + ", Id: " + req.id

我该怎么做?

由于

2 个答案:

答案 0 :(得分:3)

答案是:

Checks query string params (req.query), ex: ?id=12

答案 1 :(得分:2)

这样的事情:

var http = require('http'), url = require('url');
http.createServer(function(request, response) {
    response.writeHead(200, {"Content-Type":"text/plain"});
    var _url = url.parse(request.url, true);
    response.write("Hello " + _url.query["from"] + "!\n"); // etc: _url.query["to"]...
    response.close();
}).listen(8000);

url.parse是关键点......或者您可以使用querystring.parse(urlString)
您可以在http://nodejs.org文档部分了解更多信息。