Node.js: Read params passed in the URL

末鹿安然 提交于 2019-12-13 13:21:44

问题


In rails I do a POST request to my server:

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

In the Node.js app, I want to read From and To:

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

How do I do it?

Thanks


回答1:


The answer is:

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



回答2:


Something like this:

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 is a key point... Or you can use querystring.parse(urlString)
You can read more at http://nodejs.org docs section.



来源:https://stackoverflow.com/questions/7398264/node-js-read-params-passed-in-the-url

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!