Node.js 中未定义 Formidable

问题描述 投票:0回答:2

我正在尝试创建一个用户可以上传图像和档案的 HTTP 服务器,为此我使用

formidable
来处理上传部分。

我的代码:

requestHandler.js

 // Request Handlers (will handle all requests that happen)

    // Allows Node.js to make use of a non-blocking operation: exec ();
    var exec = require("child_process").exec;
    var querystring = require("querystring");
    fs = require("fs");
    var fomidable = require("formidable");

    function start(response, postData) {
        console.log("Request handler 'start' was called");
        var body = '<html>' +
        '<head>' +
        '<meta http-equiv = "Content-Type" content = "text/html";'+
        'charset="UTF-8"/>' +
        '</head>' +
        '<body>' +
        '<form action = "/upload" enctype = "multipart/form-data"' +
        'method = "post">' +
        '<input type ="file" name = "upload multiple = "multiple">' +
        '<input type = "submit" value ="Upload file" />' +
        '</form>' +
        '</body>' +
        '</html>';

        response.writeHead(200, {"Content-Type" : "text/html"});
        response.write(body);
        response.end();
    }

        // What is being passed between router.js and requestHandler.js is the entire body of the POST data request.

    function upload(response, request) {
        console.log("Request handler 'upload' was called");

        var form = new formidable.IncomingForm();
        console.log("about to parse");
        form.parse(request, function(error, fields, files) { 
        console.log("parsing done");

        /*Possible error on Windows systems; tried to rename to an already existing file*/

        fs.rename(files.upload.path, "/tmp/test.png", function(err) {
            if(err) {
                fs.unlink("/tmp/test.png");
                fs.rename(files.upload.path, "/tmp/test.png");
            }
        });
        response.writeHead(200, {"Content-Type" : "text/plain"});
        response.write("You've sent the text : " + querystring.parse(postData).text);
        response.write("\nHello Upload");
        response.end();
    });
    }

    function show(response) {
        console.log("Request handler 'show' was called.");
        fs.readFile("/tmp/test.png", "binary", function(error,file) {
            if(error) {
                response.writeHead(500, {"Content-Type" : "text/plain"});
                response.write(err + "\n");
                response.end();
            } else {
                response.writeHead(200, {"Content-Type" : "image/png"});
                response.write(file, "binary");
                reponse.end();
            }
        });
    }
    exports.start = start;
    exports.upload = upload;
    exports.show = show;

我也在代码的另一部分中使用

formidable
,在我的
server.js
中,但由于某种原因可以工作(因此我没有发布代码),错误指出
requestHandler.js
行35:

var form = new formidable.IncomingForm();

强大没有定义

如何定义强大?当我已经这样做时,首先打电话给:

var formidable = require("formidable");
javascript node.js formidable
2个回答
0
投票

我认为你有一个错字......

var fomidable = require("formidable");

大概应该是:

var formidable = require("formidable");

因此

formidable is not defined
...您将其定义为
fomidable


0
投票

导入文件:

const { formidable } = require("formidable");
© www.soinside.com 2019 - 2024. All rights reserved.