問題:。節點包'formidable'中的.parse()方法不想執行。formidable.parse方法不執行,nodejs和強大的包
Reasources:本網站上的代碼片段是我使用的參考是什麼,是否有幫助:https://github.com/felixge/node-formidable
說明:我想創建一個網站,可以上傳並存儲與文件nodejs用於服務器端代碼。我使用httpdispatcher包來指導用戶,並使用強大的包來處理上載表單。
我遇到了一個問題,我的程序每次都按預期執行,直到強大的包方法解析(),然後停止執行任何操作。瀏覽器選項卡試圖重新加載,並且在差不多5分鐘後顯示「服務器沒有發回任何內容」的錯誤。我沒有收到來自節點的錯誤。
顯然,這段代碼只會將處理過的表單數據發回給用戶。據我所知,我只是簡化了它。
代碼:的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>
預先感謝您。
表單的動作不應該像'action =「/ upload」'? –
@Amir我不知道,但我沒有重定向到不同的頁面?那是我需要做的嗎? –
您正在處理'/ upload'上的表單數據,所以表單應該指向那裏。或者你可以在'/ main'上處理表單。但這不是問題。 –