无法访问请求.当使用路由器时,在nodeJS中使用express 4.0

Unable to access req.files in nodeJS with express 4.0 when using a router

本文关键字:nodeJS express 路由器 访问 请求      更新时间:2023-09-26

当我尝试上传文件到我的服务器时,它失败了,出现以下错误:

        TypeError: Cannot read property 'file' of undefined
   at router.get.files (/home/n07070/Documents/Code/PicTemp/controllers/index.js:229:29)
   at Layer.handle [as handle_request] (/home/n07070/Documents/Code/PicTemp/node_modules/express/lib/router/layer.js:95:5)
   at next (/home/n07070/Documents/Code/PicTemp/node_modules/express/lib/router/route.js:131:13)
   at ensureAuthenticated (/home/n07070/Documents/Code/PicTemp/controllers/index.js:210:39)
   at Layer.handle [as handle_request] (/home/n07070/Documents/Code/PicTemp/node_modules/express/lib/router/layer.js:95:5)
   at next (/home/n07070/Documents/Code/PicTemp/node_modules/express/lib/router/route.js:131:13)
   at Route.dispatch (/home/n07070/Documents/Code/PicTemp/node_modules/express/lib/router/route.js:112:3)
   at Layer.handle [as handle_request] (/home/n07070/Documents/Code/PicTemp/node_modules/express/lib/router/layer.js:95:5)
   at /home/n07070/Documents/Code/PicTemp/node_modules/express/lib/router/index.js:277:22
   at Function.process_params (/home/n07070/Documents/Code/PicTemp/node_modules/express/lib/router/index.js:330:12)

我有我的app.js分为两部分:./controllers/中的index.js文件和主目录中的app.js文件。我这样调用app.js中的index.js:

// Load the controller
var index = require('./controllers/index');
var multer = require('multer');
app.use(multer({dest:'./writable/'}).single('file'));

app.use('/', index);

在app.js中

module.exports = router;
router.post('/upload', ensureAuthenticated, function(req, res, next) {
    var is;
    var os;
    var targetPath;
    var targetName;
    console.log(req.files);
    var tempPath = req.files.file.path;
    //get the mime type of the file
    var type = mime.lookup(tempPath);
    //get the extenstion of the file
    var extension = tempPath.split(/[. ]+/).pop();
    //check to see if we support the file type
    if (IMAGE_TYPES.indexOf(type) == -1) {
      return res.send(415, 'Supported image formats: jpeg, jpg, jpe, png.');
    }
    //create a new name for the image
    targetName = uid(22) + '.' + extension;
    //determine the new path to save the image
    targetPath = path.join(TARGET_PATH, targetName);
    //create a read stream in order to read the file
    is = fs.createReadStream(tempPath);
    //create a write stream in order to write the a new file
    os = fs.createWriteStream(targetPath);
    is.pipe(os);
    //handle error
    is.on('error', function() {
      if (err) {
        return res.send(500, 'Something went wrong');
      }
    });
    //if we are done moving the file
    is.on('end', function() {
      //delete file from temp folder
      fs.unlink(tempPath, function(err) {
        if (err) {
          return res.send(500, 'Something went wrong');
        }
        //send something nice to user
        res.render('image', {
          name: targetName,
          type: type,
          extension: extension
        });
      });//#end - unlink
    });//#end - on.end
});

我使用mutler来处理文件上传。我怎样才能解决这个问题?谢谢你。

正如@adeneo指出的,当您有多个文件时,req.files是一个数组。

您需要通过将其设置为req.files[x]来访问这些