Nodejs传递request的问题
Nodejs传递request的问题
之前学习nodejs入门遇到的问题,链接 现在问题已经解决,是因为路由模块中upload得到的request参数method属性总是"Get",加上了判断就没有问题:
function upload (response, request) {
console.log('Request handler "upload" was called.');
if (request.url == '/upload' && request.method.toLowerCase() == 'post'){
var form = new formidable.IncomingForm();
form.uploadDir = 'tmp';
form.parse(request, function(error, fields, files){
try{
console.log(request.method); //没加判定之前一直是get
fs.renameSync(files.upload.path, './tmp/test.png');
}catch(e){
console.log(e);
}
response.writeHead(200, {'Content-Type': 'text/html'});
response.write('Received image: <br>');
response.write('<img src="/show" />');
response.end();
});
}
}
上面代码第三行是根据formidable说明加上的。
虽然问题没了,但是不太明白为什么,谁能帮忙解释一下啊
Node.js 传递 request 的问题
背景
在使用 Node.js 处理 HTTP 请求时,经常会遇到 request
对象的 method
属性值不正确的情况。这个问题主要发生在处理文件上传等需要通过 POST 方法提交数据的情况下。
问题描述
在之前的代码中,upload
处理函数中的 request.method
始终返回 "GET"
,即使实际请求是 "POST"
。这导致无法正确处理文件上传逻辑,因为文件上传通常需要通过 POST 方法来提交表单数据。
解决方案
为了确保我们只在实际的 POST 请求中执行文件上传逻辑,我们需要在代码中添加一个条件判断,检查 request.method
是否为 "POST"
。以下是修改后的代码:
const formidable = require('formidable');
const fs = require('fs');
function upload(response, request) {
console.log('Request handler "upload" was called.');
if (request.url === '/upload' && request.method.toLowerCase() === 'post') {
// 创建一个 formidable 表单解析器实例
const form = new formidable.IncomingForm();
form.uploadDir = 'tmp';
// 解析请求中的表单数据
form.parse(request, function (error, fields, files) {
try {
// 确认请求方法为 POST
console.log(request.method); // 应该输出 "POST"
fs.renameSync(files.upload.path, './tmp/test.png');
} catch (e) {
console.log(e);
}
// 返回响应
response.writeHead(200, { 'Content-Type': 'text/html' });
response.write('Received image: <br>');
response.write('<img src="/show" />');
response.end();
});
}
}
代码解释
-
条件判断:
if (request.url === '/upload' && request.method.toLowerCase() === 'post')
- 这个条件确保只有当请求 URL 是
/upload
且请求方法是 POST 时,才会执行文件上传逻辑。
- 这个条件确保只有当请求 URL 是
-
表单解析:
form.parse(request, function (error, fields, files) { ... })
formidable
模块用于解析上传的表单数据。form.parse()
方法会解析请求体中的数据,并将结果传递给回调函数。
-
文件重命名:
fs.renameSync(files.upload.path, './tmp/test.png');
- 将上传的临时文件重命名为指定名称。
renameSync
是同步方法,确保文件操作完成后再继续执行后续代码。
- 将上传的临时文件重命名为指定名称。
通过这样的处理方式,我们可以确保只有在正确的请求条件下,才会执行文件上传逻辑,避免了因请求方法错误导致的问题。
建议你看看http://hi.baidu.com/zhang87224088/item/248ff113fae4546f70d5e87e
这段代码的问题在于处理HTTP请求时没有正确区分GET
和POST
请求。具体来说,formidable
库是用来解析上传表单的数据的,而它需要一个POST
请求来获取数据。
解释
request.method
: 这个属性用来判断当前请求的方法(GET
或POST
)。在文件上传的情况下,客户端通常会使用POST
方法来发送数据。if (request.url == '/upload' && request.method.toLowerCase() == 'post')
: 这里判断了两个条件:request.url == '/upload'
: 确保请求的URL路径是/upload
。request.method.toLowerCase() == 'post'
: 确保请求的方法是POST
。
示例代码
const formidable = require('formidable');
const fs = require('fs');
function upload(response, request) {
console.log('Request handler "upload" was called.');
if (request.url === '/upload' && request.method.toLowerCase() === 'post') {
const form = new formidable.IncomingForm();
form.uploadDir = 'tmp';
form.parse(request, (error, fields, files) => {
try {
console.log(request.method); // 输出应该是 POST
fs.renameSync(files.upload.path, './tmp/test.png');
} catch (e) {
console.log(e);
}
response.writeHead(200, { 'Content-Type': 'text/html' });
response.write('Received image: <br>');
response.write('<img src="/show" />');
response.end();
});
}
}
module.exports = upload;
代码解释
-
引入模块:
formidable
用于解析上传的表单数据。fs
用于文件系统操作。
-
检查请求类型:
- 检查请求的URL是否为
/upload
并且请求方法是否为POST
。
- 检查请求的URL是否为
-
创建表单对象:
- 使用
formidable
创建一个表单对象,并设置上传目录。
- 使用
-
解析请求数据:
- 使用
form.parse
方法解析请求中的数据。当数据解析完成后,将上传的文件移动到指定目录。
- 使用
-
返回响应:
- 设置响应状态码为200,设置响应头,并输出HTML以显示上传的图片。
通过这种方式,可以确保只有POST
请求才能触发文件上传逻辑。