使用 Node.js 的 HTTP PUT 请求

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7225045/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 14:30:42  来源:igfitidea点击:

HTTP PUT Request with Node.js

node.js

提问by mellowsoon

I'm trying to figure out how to make an HTTP PUT request with node.js. I've tried a lot of different things, but can't get it working.

我想弄清楚如何使用 node.js 发出 HTTP PUT 请求。我尝试了很多不同的东西,但无法让它工作。

The idea is to have a method for putting the file, eg:

这个想法是有一种放置文件的方法,例如:

function putFile(file_path, callback) {
    // Put the file
}

Any help would be appreciated.

任何帮助,将不胜感激。

采纳答案by stewe

Here is an example which sends a POSTrequest: http://nodejs.org/docs/v0.4.11/api/http.html#http.request, basically you just have to change it to PUT.

这是一个发送POST请求的示例:http://nodejs.org/docs/v0.4.11/api/http.html#http.request,基本上您只需要将其更改为PUT.

You could open your file using createReadStream()and pipe()it to the response object.

您可以使用createReadStream()pipe()打开您的文件到响应对象。

Here is another examplewhich uses readFile(), the problem with that is that the whole file is loaded into memory, so better use createReadStream()and pipe()if the files are large.

下面是另一个例子,其用途readFile(),但问题是,整个文件被加载到内存中,因此更好地利用createReadStream()pipe()如果文件比较大。