“无法设置标题......”错误

时间:2016-12-01 22:25:39

标签: javascript node.js express error-handling

以下是我正在研究的api的代码,客户端发送一个短视频文件来表达和表达上传到cloudinary。 api在第一次/ routes / upload被调用时成功将视频上传到cloudinary。当第二次调用路线时,事情开始变得有趣。视频仍然成功上传到cloudinary但是我收到以下错误:

Error: Can't set headers after they are sent.

我不明白我做错了什么,因为我确实有其他使用res.json的路线,当我多次打电话给我时,我根本没有出错。我该如何解决这个问题?

app.js

var express = require('express');
var formidable = require('express-formidable');
var app = express();

app.use(formidable());

var routes = require('./routes');
app.use('/routes', routes);

var port = process.env.PORT || 3000;

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

routes.js

var express = require('express');
var cloudinary = require('../cloudinary.js').cloudinary;
var router = express.Router();

router.post('/upload', function(req, res, next) {
  var fileGettingUploaded = req.files.fileToUpload.path;

  cloudinary.uploader.upload(fileGettingUploaded, function(result) {
    res.json(result);
  });
});

module.exports = router;

cloudinary.js

var cloudinary = require('cloudinary');

cloudinary.config({
  cloud_name: 'name',
  api_key: 'key',
  api_secret: 'secret'
});

module.exports.cloudinary = cloudinary;

完整错误

Error: Can't set headers after they are sent.
    at ServerResponse.OutgoingMessage.setHeader (_http_outgoing.js:346:11)
    at ServerResponse.header (/Users/myname/appdirectory/node_modules/express/lib/response.js:719:10)
    at ServerResponse.send (/Users/myname/appdirectory/node_modules/express/lib/response.js:164:12)
    at ServerResponse.json (/Users/myname/appdirectory/node_modules/express/lib/response.js:250:15)
    at /Users/myname/appdirectory/app.js:82:7
    at Layer.handle_error (/Users/myname/appdirectory/node_modules/express/lib/router/layer.js:71:5)
    at trim_prefix (/Users/myname/appdirectory/node_modules/express/lib/router/index.js:310:13)
    at /Users/myname/appdirectory/node_modules/express/lib/router/index.js:280:7
    at Function.process_params (/Users/myname/appdirectory/node_modules/express/lib/router/index.js:330:12)
    at next (/Users/myname/appdirectory/node_modules/express/lib/router/index.js:271:10)
    at /Users/myname/appdirectory/app.js:76:3
    at Layer.handle [as handle_request] (/Users/myname/appdirectory/node_modules/express/lib/router/layer.js:95:5)
    at trim_prefix (/Users/myname/appdirectory/node_modules/express/lib/router/index.js:312:13)
    at /Users/myname/appdirectory/node_modules/express/lib/router/index.js:280:7
    at Function.process_params (/Users/myname/appdirectory/node_modules/express/lib/router/index.js:330:12)
    at next (/Users/myname/appdirectory/node_modules/express/lib/router/index.js:271:10)
    at form.parse (/Users/myname/appdirectory/node_modules/express-formidable/lib/middleware.js:17:7)
    at IncomingForm.<anonymous> (/Users/myname/appdirectory/node_modules/formidable/lib/incoming_form.js:105:9)
    at emitNone (events.js:91:20)
    at IncomingForm.emit (events.js:185:7)
    at IncomingForm._maybeEnd (/Users/myname/appdirectory/node_modules/formidable/lib/incoming_form.js:553:8)
    at /Users/myname/appdirectory/node_modules/formidable/lib/incoming_form.js:230:12

0 个答案:

没有答案