如何使用 node.js 发出与此代码等效的 Http 请求:
curl -X PUT http://localhost:3000/users/1
最佳答案
对于其他在谷歌上搜索此问题的人,接受的答案不再正确并且已被弃用。
正确的方法(在撰写本文时)是使用 http.request 方法,如下所述:nodejitsu example
代码示例(来自上面的文章,已修改以回答问题):
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();
关于javascript - 带有 Node 的 Http 请求?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4294939/