Nodejs如何使用http.request()来封装一个请求,并且实现把文件上传到另一台服务器?
Nodejs如何使用http.request()来封装一个请求,并且实现把文件上传到另一台服务器?
exports.uploadFileToServer = function(filepath) {
var headers = { //定义符合上传API的请求头
‘Content-Type’: ‘multipart/form-data;boundary=------------ei4Ij5Ef1ae0ae0Ij5Ij5Ij5Ij5GI3’,
‘Content-Length’: ‘1049’, ‘Connection’: ‘Keep-Alive’, ‘Pragma’, ‘no-cache’ };
var reqos = {
host: ‘xxx.com’,
port: 80,
path: ‘/fileupload’,
method: ‘POST’,
headers: headers
};
var req = http.request(reqos, function(res) {
console.log('STATUS: ’ + res.statusCode);
console.log('HEADERS: ’ + JSON.stringify(res.headers));
res.setEncoding(‘utf8’);
res.on(‘data’, function (chunk) { //得到返回结果
console.log('BODY: ’ + chunk);
});
});
// 向 request 体里面写入数据,只能是 string 或者 buffer
//req.write(filepath);
//我们要上传一个文件,所以要用到读取流
//创建一个文件读取流
var readStream = fs.createReadStream(filepath);
readStream.on(‘open’, function () {
// 等待读取流打开,然后写入到 req 对象中
readStream.pipe(req);
});
// 捕获错误
readStream.on(‘error’, function(err) {
req.end(err);
});
req.end();
}
要实现使用 Node.js 的 http.request()
方法来封装一个请求,并将文件上传到另一台服务器,可以参考以下步骤和示例代码:
示例代码
const http = require('http');
const fs = require('fs');
exports.uploadFileToServer = function(filepath) {
const headers = {
'Content-Type': 'multipart/form-data; boundary=------------ei4Ij5Ef1ae0ae0Ij5Ij5Ij5Ij5GI3',
'Content-Length': '1049',
'Connection': 'Keep-Alive',
'Pragma': 'no-cache'
};
const reqOptions = {
host: 'xxx.com',
port: 80,
path: '/fileupload',
method: 'POST',
headers: headers
};
const req = http.request(reqOptions, function(res) {
console.log('STATUS: ' + res.statusCode);
console.log('HEADERS: ' + JSON.stringify(res.headers));
res.setEncoding('utf8');
res.on('data', function(chunk) {
console.log('BODY: ' + chunk);
});
});
// 创建一个文件读取流
const readStream = fs.createReadStream(filepath);
// 当读取流打开时,将其内容写入请求体
readStream.on('open', function() {
readStream.pipe(req);
});
// 捕获读取流的错误
readStream.on('error', function(err) {
req.end(err);
});
// 结束请求
req.end();
};
解释
-
引入模块:
http
模块用于发起 HTTP 请求。fs
模块用于文件系统操作。
-
定义请求头:
headers
对象包含了请求头信息。注意Content-Type
设置为multipart/form-data
并指定了一个边界字符串。
-
配置请求选项:
reqOptions
包含了目标主机、端口、路径以及请求方法等信息。
-
发起请求:
- 使用
http.request()
发起一个 POST 请求。 - 在请求的回调函数中处理响应数据。
- 使用
-
读取文件并写入请求体:
- 使用
fs.createReadStream()
创建一个文件读取流。 - 监听
open
事件,在文件流打开后通过pipe
方法将文件内容写入请求体。
- 使用
-
错误处理:
- 监听
error
事件,捕获并处理可能发生的错误。
- 监听
-
结束请求:
- 最后调用
req.end()
结束请求。
- 最后调用
这个示例展示了如何使用 Node.js 的 http.request()
方法来上传文件到远程服务器。
multipart/form-data 的body部分不是纯的文件内容,要有boundary(就是header中的),去看看相关规范吧。
嗯,needle确实封装的不错,学习中……
request模块吧,用着挺好 https://github.com/mikeal/request
嗯,这个也不错,我比较看哪个更好,谢谢提供……
原来这么多啊……
为了将文件上传到另一台服务器,我们可以使用 Node.js 的 http.request
方法。下面是一个具体的实现方法:
const http = require('http');
const fs = require('fs');
exports.uploadFileToServer = function(filepath) {
const headers = {
'Content-Type': 'multipart/form-data',
'Content-Length': fs.statSync(filepath).size
};
const reqos = {
host: 'xxx.com',
port: 80,
path: '/fileupload',
method: 'POST',
headers: headers
};
const req = http.request(reqos, function(res) {
console.log('STATUS: ' + res.statusCode);
console.log('HEADERS: ' + JSON.stringify(res.headers));
res.setEncoding('utf8');
res.on('data', function(chunk) {
console.log('BODY: ' + chunk);
});
});
// 创建一个可读流
const readStream = fs.createReadStream(filepath);
// 将读取流绑定到请求流上
readStream.pipe(req);
// 捕获错误
readStream.on('error', function(err) {
req.end();
console.error(err);
});
// 请求结束时关闭读取流
req.on('end', function() {
readStream.close();
});
};
解释:
- 我们首先引入了
http
和fs
模块。 - 定义了一个
uploadFileToServer
函数,该函数接收文件路径作为参数。 - 设置了请求头
headers
,其中Content-Type
为multipart/form-data
,Content-Length
为文件的大小。 - 定义了请求对象
reqos
,包含了目标服务器的主机名、端口、路径以及 HTTP 方法等信息。 - 使用
http.request
方法发起 POST 请求。 - 创建了一个文件读取流
readStream
,并将其与请求流关联起来,以便将文件内容上传至服务器。 - 添加了错误处理机制,确保在发生错误时能够及时关闭读取流。
- 最后,在请求结束时关闭读取流。
注意:实际使用时需要根据具体需求调整 Content-Type
和 boundary
等参数。