我用模块表达强大

当我加载页面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坐镇,奉献自若,sius do eiusmod tempor incididunt ut labour et dolore magna aliqua。尽量不要抽烟,不要因抽烟而锻炼。 Duis aute irure dolor in reprehenderit in voltate velit esse cillum dolore eu fugiat nulla pariatur。圣人occaecat cupidatat非偶然的,在犯错的情况下动手动手做工的

Sed ut perspiciatis unde omnis iste natus error sit voluptatem被告doloremque laudantium,totam rem aperiam,eaque ipsa quae ab illo inventore veritatis et quasi architecto beatae vitae dicta cteta explicabo。 Nemo enim ipsam voluptatem quia voluptas坐在aspernatur aut odt aut fugit,sed quia consequuntur magni dolores eos qui ratio than voluptatem sequi nesciunt。 Neque porro quisquam est,qui dolorem ipsum quia dolor set amet,consectetur,adipisci velit,sed quia non numquam eius modi tempora incidunt ut laboure and dolore magnam aliquam quaerat voluptatem。是否需要最低限度的抽烟,是否需要进行实验室锻炼,是否需要临时服药?完全替代quilit autem vel eum iure reprehenderit qui,velit qui qui dolorem eum fugiat quo voluptas nulla pariatur

最佳答案

有一个解决方案,中间件版本中有可表达的问题,

每个事件处理程序都有两个,这意味着每个事件被处理两次,从而导致两个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);

10-05 22:29