express-Formidable "发送后无法设置标题。"
Posted
技术标签:
【中文标题】express-Formidable "发送后无法设置标题。"【英文标题】:express-Formidable "Can't set headers after they are sent." 【发布时间】:2018-02-21 08:40:36 【问题描述】:我使用的模块很强大
当我加载第 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
不要阅读下一段文字,这只是为了消除***错误!!!我不知道如何为这个问题添加更多信息。
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 reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur。 Exceptioneur 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 illoinvente 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 eum iure reprehenderit qui in ea voluptate velit esse quam nihil Molestiae consequatur, vel illum qui dolorem eum fugiat quo voluptas nulla pariatur
【问题讨论】:
【参考方案1】:有一个解决办法,express-formidable 中间件版本有问题,
每个事件处理程序有两个,这意味着每个事件被处理两次,导致两次 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);
【讨论】:
在express-formidable
repo 中,该问题自 2 月以来已得到修复,但 NPM 包未更新。在那之前,直接使用formidable
或者安装修复它的特定提交:"github:utatti/express-formidable#9986be6279"
。以上是关于express-Formidable "发送后无法设置标题。"的主要内容,如果未能解决你的问题,请参考以下文章
如何将 ["tag":"google","count":1,"tag":"microsoft",&q
"message":"请求失败,状态码 500","name":"Error","stack":&q
"errors":"errors":["detail":"您无权执行此操作。","code":&qu
"Navicat" , "mysql" , "xxx.war" , "tomcat",“JDK","