gpt4 book ai didi

javascript - formidable.parse 方法未执行,nodejs 和 formidable 包

转载 作者:太空宇宙 更新时间:2023-11-04 00:32:32 24 4
gpt4 key购买 nike

问题: Node 包“formidable”中的 .parse() 方法不想执行。

资源:此网站上的代码片段是我用来引用的,如果有帮助的话:https://github.com/felixge/node-formidable

描述:我正在尝试创建一个网站,可以使用nodejs为服务器端代码上传和存储文件。我使用 httpdispatcher 包来指导用户,并使用强大的包来处理上传表单来实现这一点。

我遇到一个问题,每次我的程序都会按预期执行,直到强大的包方法 parse() 为止,然后停止执行任何操作。浏览器选项卡尝试重新加载,大约 5 分钟后显示“服务器未发回任何内容”的错误。我没有收到来自 Node 的错误。

显然,这段代码只是将处理后的表单数据发送回用户。我只是在我认为合理的范围内进行了简化。

代码:nodejs,(server.js文件):

var http = require('http');
console.log('http loaded');
var fs = require('fs');
console.log('fs loaded');
var dispatcher = require('httpdispatcher');
console.log('dispatcher loaded');

var formidable = require('formidable');
console.log('formidable loaded');
var util = require('util');
console.log('util loaded');

dispatcher.setStaticDirname('.');
dispatcher.setStatic('resources');
dispatcher.onGet('/main', function(request, response){
response.writeHead(200, {'Content-Type': 'text/html'});
fs.readFile('index.html', function(error, html){
response.end(html); //Callbacked to prevent async send of file read
})
});
dispatcher.onPost('/main', function(request, response){
console.log('upload url accessed');
processForm(request, response);
});

const PORT = 8000;

function processForm(request, response){
console.log('processForm launched');
var form = new formidable.IncomingForm();
console.log('formidable initailised');
form.parse(request, function(error, fields, files){
console.log('form parsing started');
response.writeHead(200, {'content-type': 'text/plain'});
console.log('head written');
response.write('data recieved:\n\n');
response.end(util.inspect({fields: fields, files: files}));
console.log('form passing ended');
});
}
function saveImage(){
console.log('saveImage called');
fs.readFile(request.files.uploadImage.path, function(error, imageData){
console.log('reading file');
path = './uploadedImages';
fs.writeFile(path, imageData, function(error){
console.log('fs encountered an error: %s', error);
});
});
console.log('saveImage ended');
}
function handleRequest(request, response){
console.log('request handler started');
console.log('request method: %s', request.method);
try{
console.log(`URL: %s was requested`, request.url);
dispatcher.dispatch(request, response);
}
catch(error){
console.log(`httpdispatcher encountered an error: %s`, error);
}
console.log('request handler ended');
}

var server = http.createServer(handleRequest);
server.listen(PORT, function(){
//Callback triggered when server is successfully listening.
console.log("Server listening on: http://localhost:%s", PORT);
});

代码:html,(index.html文件):

<!DOCTYPE html>
<html>
<head></head>
<body>
<form action="" method="post" enctype="multipart/form-data">
<input type="file" name="uploadImage">
</form>
<form action="" enctype="multipart/form-data" method="post">
<fieldset>
<label for="imageTitle">Image title:</label>
<input type="text" id="imageTitle" name="imageTitle" placeholder="Enter title of image" />
<br />
<label for="imageDesription">Image desription:</label>
<textarea id="imageDesription" name="imageDesription" placeholder="Enter a description of the image"></textarea>
<br />
<input type="submit" value="Upload Image" />
</fieldset>
</form>
</body>
</html>

提前谢谢您。

最佳答案

看起来像httpdispatcher修改请求缓冲区,使强大的无法从中提取表单值。

要测试这一点,您可以直接在 http 模块的回调中处理 post 请求,而无需分派(dispatch)请求:

function handleRequest(request, response){
console.log('request handler started');
console.log('request method: %s', request.method);

// HERE
if (request.method == 'POST') {
processForm(request, response);
return;
}

try{
console.log(`URL: %s was requested`, request.url);
dispatcher.dispatch(request, response);
}
catch(error){
console.log(`httpdispatcher encountered an error: %s`, error);
}
console.log('request handler ended');
}

我建议您使用express根据方法和路由发送请求。

关于javascript - formidable.parse 方法未执行,nodejs 和 formidable 包,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40434861/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com