【问题标题】:Express route with specific middleware using Express.Router()使用 Express.Router() 使用特定中间件的快速路由
【发布时间】:2016-03-06 05:06:11
【问题描述】:

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

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
    }

})();

【问题讨论】:

  • 如何用exports.post替换module.exports并相应地调整其余代码
  • 为什么要使用自调用函数?
  • @Vohuman 我看到一个这样使用它的例子,是不是很糟糕?
  • 还不错。此处无需使用。
  • 那么推荐的重写方式是什么@Vohuman?

标签: node.js express middleware multer


【解决方案1】:

您以不正确的方式使用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 
})

【讨论】:

  • 谢谢,现在我遇到了另一个问题,我上传的文件是 pdf,但是保存时它没有扩展名(单个文件上传)这是文件 json:{ fieldname: 'file', originalname: 'Ejemplos.pdf', encoding: '7bit', mimetype: 'application/pdf', destination: './client/public/docs/transactions/', filename: '32d8d06c38475b339e68b2a640fcc359', path: 'client\\public\\docs\\transactions\\32d8d06c38475b339e68b2a640fcc359', size: 484581 }
  • 我是否必须将扩展名和名称属性从 Angular 添加到 json 以便使用另一个名称和正确的扩展名保存它?
  • 我看到您为此发布了另一个问题。将尝试在那里回答。
猜你喜欢
  • 2019-02-18
  • 1970-01-01
  • 2016-02-14
  • 1970-01-01
  • 2018-09-27
  • 1970-01-01
  • 2018-07-15
  • 2013-09-22
  • 2017-07-03
相关资源
最近更新 更多