node.js - 使用 Express.Router() 具有特定中间件的快速路由

标签 node.js express middleware multer

我使用express router来定义我的路由,我需要添加一个中间件,以便一些路由,只需在回调函数输出以下错误之前添加中间件:

Error: Route.post() requires callback functions but got a [object Object]

我正在使用文件夹作为模块,我的模块正在上传,这是index.js:

module.exports = (function () {

    var express          = require( 'express' ),
        router           = express.Router(),
        multer           = require( 'multer' ),
        transaction_docs = multer( { dest: './client/public/docs/transactions/' } ),
        product_images   = multer( { dest: './client/public/img/products' } ),
        upload           = require( './upload' );

    router.route( '/upload/transaction-docs' )
        .post( transaction_docs, upload.post );//If I take off transaction_docs theres no error

    router.route( '/upload/product-images' )
        .post( product_images, upload.post );//Same as above

    return router;

})();

这是upload.js:

module.exports = (function () {

    function post( request, response ) {

        var filesUploaded = 0;

        if ( Object.keys( request.files ).length === 0 ) {
            console.log( 'no files uploaded' );
        } else {
            console.log( request.files );

            var files = request.files.file1;
            if ( !util.isArray( request.files.file1 ) ) {
                files = [ request.files.file1 ];
            }

            filesUploaded = files.length;
        }

        response.json(
            {
                message: 'Finished! Uploaded ' + filesUploaded + ' files.',
                uploads: filesUploaded
            }
        );

    }

    return {
        post: post
    }

})();

最佳答案

您使用multer的方式不正确。对于中间件,您不能直接使用 transaction_docs 或 Product_images,因为它们不是函数。

由于您计划上传多个文件,因此需要使用 transaction_docs.array(fieldName[, maxCount]) 这意味着它将接受一组文件,所有文件的名称均为 fieldname 。如果上传的文件超过 maxCount,则可能会出错。文件数组将存储在 req.files 中。

示例:

var express = require('express')
var multer  = require('multer')
var upload = multer({ dest: 'uploads/' })

var app = express()

app.post('/profile', upload.single('avatar'), function (req, res, next) {
  // req.file is the `avatar` file 
  // req.body will hold the text fields, if there were any 
})

app.post('/photos/upload', upload.array('photos', 12), function (req, res, next) {
  // req.files is array of `photos` files 
  // req.body will contain the text fields, if there were any 
})

var cpUpload = upload.fields([{ name: 'avatar', maxCount: 1 }, { name: 'gallery', maxCount: 8 }])
app.post('/cool-profile', cpUpload, function (req, res, next) {
  // req.files is an object (String -> Array) where fieldname is the key, and the value is array of files 
  // 
  // e.g. 
  //  req.files['avatar'][0] -> File 
  //  req.files['gallery'] -> Array 
  // 
  // req.body will contain the text fields, if there were any 
})

关于node.js - 使用 Express.Router() 具有特定中间件的快速路由,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34034581/

相关文章:

javascript - 在 node.js 代码中实现回调的问题

ruby - Rails 5 升级:在 ActionDispatch::ParamsParser (RuntimeError) 之后没有要插入的中间件

javascript - 如何为express.js 应用程序创建 super 管理员帐户?

node.js - 解析 Post 表单数据 Node.js Express

asp.net-mvc - 从 OWIN 身份验证中间件考虑 Controller 属性

node.js - 安装 Node 模块时 node-gyp 重建错误

javascript - 使用nodejs中的fast-csv根据列值将csv文件拆分为多个csv文件

node.js - 我可以使用swank-js在js2模式下完成node.js API吗?

javascript - Chrome远程接口(interface): How to Wait Until a New Page Loads

node.js - 使用 JADE、属性 "placeholder"和输入字段的正确方法是什么?