简体   繁体   English

表达中间件错误

[英]Express middleware error

I'm making an API that accepts a video and uploads it to cloudinary. 我正在制作一个接受视频的API并将其上传到cloudinary。 Meta data about the upload is then returned to the user. 然后将关于上载的元数据返回给用户。 The problem that I am having occurs when I try to handle the video in middleware. 当我尝试在中间件中处理视频时,我遇到了这个问题。 I'm not sure why I keep getting this error. 我不知道为什么我一直收到这个错误。

The error: 错误:

/Users/name/test/cloudinary_upload/node_modules/express/lib/router/route.js:196
        throw new Error(msg);
        ^

Error: Route.post() requires callback functions but got a [object Object]
    at Route.(anonymous function) [as post] (/Users/name/test/cloudinary_upload/node_modules/express/lib/router/route.js:196:15)
    at Function.proto.(anonymous function) [as post] (/Users/name/test/cloudinary_upload/node_modules/express/lib/router/index.js:510:19)
    at Object.<anonymous> (/Users/name/test/cloudinary_upload/routes.js:8:8)
    at Module._compile (module.js:541:32)
    at Object.Module._extensions..js (module.js:550:10)
    at Module.load (module.js:458:32)
    at tryModuleLoad (module.js:417:12)
    at Function.Module._load (module.js:409:3)
    at Module.require (module.js:468:17)
    at require (internal/module.js:20:19)

PROJECT CODE BELOW: 以下项目代码:

app.js app.js

'use strict';

var express = require('express');
var formidable = require('express-formidable');
var port = process.env.PORT || 3000;
var routes = require('./routes');
var app = express();

// Parse incoming request
app.use(formidable());

app.use('/routes', routes);

// Catch 404 and forward to error handler
app.use(function(req, res, next) {
  var err = new Error('Not Found');
  err.status = 404;
  next(err);
});

// Error handler
app.use(function(err, req, res, next) {
  res.status(err.status || 500);
  res.json({
    error: {
      message: err.message
    }
  });
});


app.listen(port, function() {
  console.log('Express server is listening on port ' + port);
});

routes.js routes.js

'use strict';

var express = require('express');
var createThumbnail = require('./middleware/generatevideothumbnail');
var uploadToCloudinary = require('./middleware/uploadtocloudinary');
var router = express.Router();

router.post('/upload', uploadToCloudinary, createThumbnail, function(req, res, next) {
  console.log(req.cloudinaryObject);
  console.log(req.videoThumbnail);
  res.json(req.cloudinaryObject);
});

module.exports = router;

uploadtocloudinary.js uploadtocloudinary.js

'use strict';

var cloudinary = require('../cloudinary_config.js').cloudinary;

/************************************************************
MIDDLEWARE: Accepts the request object. Uploads the video
file to cloudinary. Passes the video's public id, version, file
type, time of upload, file size and url to the next middleware
on the request object.
 ************************************************************/
function uploadToCloudinary(req, res, next) {
  cloudinary.uploader.upload(req.files.fileToUpload.path, function(resultOfUpload) {
    var cloudinaryObject = {
       public_id: resultOfUpload.public_id,
       version: resultOfUpload.version,
       typeOfFile: resultOfUpload.format, // Type of file
       uploadTime: resultOfUpload.created_at, // Upload time
       fileSize: resultOfUpload.bytes, // File size
       videoUrl: resultOfUpload.secure_url // Video URL
     }

    req.cloudinaryObject = cloudinaryObject;

    next();
  }, {
    resource_type: "video"
  });
}

module.exports.uploadToCloudinary = uploadToCloudinary;

generatevideothumbnail.js generatevideothumbnail.js

'use strict';

/************************************************************
MIDDLEWARE: Accepts the request object. Use the version and
public id of video to create thumbnail url string. Pass
thumbnail url to next middleware on the request object.
 ************************************************************/
 function generateVideoThumbnail(req, res, next) {
  req.videoThumbnail = "https://res.cloudinary.com/xxxxxxxxx/video/upload/v"
                       + req.cloudinaryObject.version
                       + "/"
                       + req.cloudinaryObject.public_id
                       + ".jpg";

  next()
}

module.exports.generateThumbnail = generateVideoThumbnail;

There are a few different choices of what to send with your required file when you use module.exports . 使用module.exports时,使用所需文件发送的内容有几种不同的选择。 You can send an entire object, or you can send specific functions. 您可以发送整个对象,也可以发送特定功能。

With module.exports.uploadToCloudinary= and module.exports.generateThumbnail= you're sending an object that includes the functions uploadToCloudinary and generateThumbnail . 使用module.exports.uploadToCloudinary=module.exports.generateThumbnail=您正在发送一个包含函数uploadToCloudinarygenerateThumbnail的对象。 When you require it with, for example, 例如,当您需要它时

var createThumbnail = require('./middleware/generatevideothumbnail'); 

you are able to use the function generateThumbnail on the object you're exporting. 您可以在要导出的对象上使用generateThumbnail函数。 In other words, to access the function you want, you'd have to write: 换句话说,要访问您想要的功能,您必须写:

 createThumbnail.generateThumbnail

If you just want to be able to use only one specific function when you bring in the required file, just give the name of the function to module.exports , like this: 如果您只想在引入所需文件时只能使用一个特定函数,只需将该函数的名称提供给module.exports ,如下所示:

module.exports = generateVideoThumbnail;

Reference: https://www.sitepoint.com/understanding-module-exports-exports-node-js/ and https://nodejs.org/api/modules.html 参考: https //www.sitepoint.com/understanding-module-exports-exports-node-js/https://nodejs.org/api/modules.html

Let me know if this makes sense. 如果这是有道理的,请告诉我。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM