ajs*_*sie 15 javascript http httpclient node.js
如何使用与此代码等效的node.js发出Http请求:
curl -X PUT http://localhost:3000/users/1
Run Code Online (Sandbox Code Playgroud)
Cla*_*ick 35
对于搜索此问题的其他人,已接受的答案已不再正确,已被弃用.
正确的方法(在撰写本文时)是使用如下所述的http.request方法:nodejitsu示例
代码示例(来自上面的文章,修改为回答问题):
var http = require('http');
var options = {
host: 'localhost',
path: '/users/1',
port: 3000,
method: 'PUT'
};
callback = function(response) {
var str = '';
//another chunk of data has been recieved, so append it to `str`
response.on('data', function (chunk) {
str += chunk;
});
//the whole response has been recieved, so we just print it out here
response.on('end', function () {
console.log(str);
});
}
http.request(options, callback).end();
Run Code Online (Sandbox Code Playgroud)
Swi*_*ler 22
使用http客户端.
这些方面的东西:
var http = require('http');
var client = http.createClient(3000, 'localhost');
var request = client.request('PUT', '/users/1');
request.write("stuff");
request.end();
request.on("response", function (response) {
// handle the response
});
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
11036 次 |
| 最近记录: |