javascript - 无法在 Nodejs 中执行文件上传程序

标签 javascript node.js single-page-application

我有一个简单的程序,我试图从 http://www.tutorialspoint.com/nodejs/nodejs_express_framework.htm 的在线教程中执行它。但我遇到了如下异常:

我对 Nodejs 比较陌生,非常感谢任何形式的帮助或指导。

使用 NodeJS 版本:4.1.1。

异常

TypeError: app.use() requires middleware functions
    at EventEmitter.use (c:\nital\my-data\my-sample-apps\nodejs\tutorialspoint-samples\node_modules\express\lib\application.js:209:11)
    at Object.<anonymous> (c:\nital\my-data\my-sample-apps\nodejs\tutorialspoint-samples\Sample37.js:11:5)
    at Module._compile (module.js:434:26)
    at Object.Module._extensions..js (module.js:452:10)
    at Module.load (module.js:355:32)
    at Function.Module._load (module.js:310:12)
    at Function.Module.runMain (module.js:475:10)
    at startup (node.js:117:18)
    at node.js:951:3

FileUploadDemo.js

var express = require('express');
var app = express();
var fs = require("fs");

var bodyParser = require('body-parser');
var multer = require('multer');

app.use(express.static('public'));
app.use(bodyParser.urlencoded({extended: false}));
app.use(multer({dest: 'C:/tmp/'}));

app.get('/upload.html', function (req, res) {
    res.sendFile(__dirname + "/" + "upload.html");
});

//uploading the file here
app.post('/file_upload', function (req, res) {
    console.log(req.files.file.name);
    console.log(req.files.file.path);
    console.log(req.files.file.type);

    var file = __dirname + "/" + req.files.file.name;
    fs.readFile(req.files.file.path, function (err, data) {
        fs.writeFile(file, data, function (err) {
            if (err) {
                console.log(err);
            } else {
                response = {
                    message: 'File uploaded successfully',
                    filename: req.files.file.name
                };
            }
            console.log(response);
            res.end(JSON.stringify(response));
        });
    });
});

var server = app.listen(8081, function () {
    var host = server.address().address;
    var port = server.address().port;
    console.log("Example app listening at http://%s:%s", host, port)
});

最佳答案

看来multer的api已经改变了。由于本教程使用 npm install multer --save ,因此它会提取最新版本,而不是确保您拥有的版本兼容。有关如何使用它的详细信息可以在 npm page 上找到。 。看来您现在需要将所需的特定中间件添加到特定路由中:

// Remove this line: app.use(multer({dest: 'C:/tmp/'}));
// Put this in instead
var upload = multer({ dest: 'C:/tmp/' });

// Change this: app.post('/file_upload', function (req, res) {
// to
app.post('/file_upload', upload.single('file'), function (req, res) {

虽然您可能仍然可以使用 app.use 将其添加到所有路由,但您肯定需要将其更改为使用 upload.single (或他们选择的其他中间件)。

关于javascript - 无法在 Nodejs 中执行文件上传程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32998993/

相关文章:

ajax - AngularJS : single page and multipage app difference and use in practice?

javascript - 在我的 SPA 中的何处添加 JQuery 多选脚本?

javascript - 如何使用ExtJS现代网格行扩展器插件?

Node.js 全局变量不起作用

javascript - Greasemonkey 和 Gmail - 解析邮件内容

node.js - Mongoose 中的 concat 和 save 没有任何作用

mysql - Socket.IO 抛出类型错误

jquery - 优化网页视频加载

javascript - Jquery,在 IE 中从 div 获取 .text()

javascript - WordPress中如何仅在用户滚动页面时显示菜单