express-Formidable“发送后不能设置标题。”

时间:2017-09-12 19:21:57

标签: node.js express formidable

我使用模块快速强大的

当我加载页面2次我在控制台上有

GET /users 304 6.948 ms - -
GET /users 304 213.344 ms - -
Error: Can't set headers after they are sent.
at validateHeader (_http_outgoing.js:489:11)
at ServerResponse.setHeader (_http_outgoing.js:496:3)
at ServerResponse.header (/home/neolinux/Encoding-Service/encode-service/node_modules/express/lib/response.js:730:10)
at ServerResponse.contentType (/home/neolinux/Encoding-Service/encode-service/node_modules/express/lib/response.js:558:15)
at ServerResponse.send (/home/neolinux/Encoding-Service/encode-service/node_modules/express/lib/response.js:145:14)
at done (/home/neolinux/Encoding-Service/encode-service/node_modules/express/lib/response.js:967:10)
at Object.exports.renderFile (/home/neolinux/Encoding-Service/encode-service/node_modules/pug/lib/index.js:422:12)
at View.exports.__express [as engine] (/home/neolinux/Encoding-Service/encode-service/node_modules/pug/lib/index.js:465:11)
at View.render (/home/neolinux/Encoding-Service/encode-service/node_modules/express/lib/view.js:127:8)
at tryRender (/home/neolinux/Encoding-Service/encode-service/node_modules/express/lib/application.js:640:10)

这是我的app.js

var express = require('express');
var path = require('path');
var favicon = require('serve-favicon');
var logger = require('morgan');
var cookieParser = require('cookie-parser');


var index = require('./routes/index');
var users = require('./routes/users');

const util=require("util");
var app = express();

const formidable = require('express-formidable');

// view engine setup
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'pug');

// uncomment after placing your favicon in /public
//app.use(favicon(path.join(__dirname, 'public', 'favicon.ico')));
app.use(logger('dev'));

app.use(cookieParser());
app.use(express.static(path.join(__dirname, 'public')));

app.use(formidable({
    uploadDir:  path.join(__dirname, '/Upload'),
}));

app.use('/', index);
app.use('/users', users);

// 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) {
  // set locals, only providing error in development
  res.locals.message = err.message;
  res.locals.error = req.app.get('env') === 'development' ? err : {};

  // render the error page
  res.status(err.status || 500);
  res.render('error');
});

module.exports = app;

你现在必须,如果我删除强大的我没有错误。有什么想法吗?

表达v 4.5 节点v8

不要读下一个文本,这只是为了删除stackoverflow错误!我不知道如何为这个问题添加更多信息。

Lorem ipsum dolor sit amet,consectetur adipiscing elit,sed do eiusmod tempor incididunt ut labore et dolore magna aliqua。 Ut enim ad minim veniam,quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat。 Duis aute irure dolor in repreptderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur。 Excepteur sint occaecat cupidatat non proident,sunt in culpa qui officia deserunt mollit anim id est laborum

Sed ut perspiciatis unde omnis iste natus error sit voluptatem accusantium doloremque laudantium,totam rem aperiam,eaque ipsa quae ab illo inventore veritatis et quasi architecto beatae vitae dicta sunt explicabo。 Nemo enim ipsam voluptatem quia voluptas sit aspernatur aut odit aut fugit,sed quia consequuntur magni dolores eos qui ratione voluptatem sequi nesciunt。 Neque porro quisquam est,qui dolorem ipsum quia dolor sit amet,consectetur,adipisci velit,sed quia non numquam eius modi tempora incidunt ut labore et dolore magnam aliquam quaerat voluptatem。 Ut enim ad minima veniam,quis nostrum exercitationem ullam corporis suscipit laboriosam,nisi ut aliquid ex ea commodi consequatur? Quis autem vel eure iure reprehenderit qui in ea voluptate velit esse quam nihil molestiae consequatur,vel illum qui dolorem eum fugiat quo voluptas nulla pariatur

1 个答案:

答案 0 :(得分:1)

有一个解决方案,快速强大的中间件版本存在问题,

每个事件处理程序有两个,这意味着每个事件被处理两次,导致两个next()调用,因此有关标题设置太晚的例外。

解决方案在app.js:

const formidable = require('formidable');
app.use(function (req, res, next) {
    var form = new formidable.IncomingForm({
        encoding: 'utf-8',
        uploadDir:  path.join(__dirname, '/Upload'),
        multiples: true,
        keepExtensions: true
    })
    form.once('error', console.log)
    form.parse(req, function (err, fields, files) {
        Object.assign(req, {fields, files});
        next();
    })
})
app.use('/', index);