使用 Node.js 的 HTTP PUT 请求 [英] HTTP PUT Request with Node.js
问题描述
我想弄清楚如何使用 node.js 发出 HTTP PUT 请求.我尝试了很多不同的东西,但无法让它发挥作用.
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.
这个想法是有一个放置文件的方法,例如:
The idea is to have a method for putting the file, eg:
function putFile(file_path, callback) {
// Put the file
}
任何帮助将不胜感激.
推荐答案
以下是发送 POST
请求的示例:http://nodejs.org/docs/v0.4.11/api/http.html#http.request ,基本上你只需将其更改为 PUT
.
Here is an example which sends a POST
request: http://nodejs.org/docs/v0.4.11/api/http.html#http.request , basically you just have to change it to PUT
.
您可以使用 createReadStream()打开您的文件a> 和 pipe() 到响应对象.
You could open your file using createReadStream() and pipe() it to the response object.
这是另一个使用 的示例readFile()
,问题是整个文件都加载到内存中,所以如果文件是,最好使用 createReadStream()
和 pipe()
大.
Here is another example which 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.
这篇关于使用 Node.js 的 HTTP PUT 请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!