Jeong Ki Hun Jeong Ki Hun - 10 months ago 81
Node.js Question

Node.js 344 error Can't set headers after they are sent

Trying to wrap my head around this error I get, I don't understand why. I seen a couple of posts with it, but they are all different cases and none kinda match mine. So appreciate help.
what i want to upload multiple files upload.
but I do file upload It's work just one.
but when i try to upload multiple files, it's not working
can you give me a answer?'/contents/insert/upload', ensureAuthenticated, function(req, res, next) {

var form = new formidable.IncomingForm();

// form.on("fileBegin", function (name, file){
// console.log('upload come on3');
// });
form.on("file", function (name, file){
fs.readFile(file.path, function(error, data){
var filePath = __dirname + '/../public/uploads/' +;

fs.writeFile(filePath, data, function(error){
throw err;
}else {


<form action="/adm/contents/insert/upload" method="post" enctype="multipart/form-data" >
<!-- <input type="file" name="file" />
<input type="submit" /> -->
<div class="file-field input-field">
<div class="btn">
<span>input images</span>
<input type="file" name="file" multiple>
<div class="file-path-wrapper">
<input class="file-path validate" type="text">
<input type="submit" class="btn waves-effect waves-light" value="upload" />


The file event will be triggered for every file in the upload, so eventually your code will issue res.redirect() for each uploaded file. This will cause the error (you can only issue a redirect, or send back a response, just once in the lifetime of a request).

Instead, you want to listen for the end event, and issue the redirect there:

form.on("file", function (name, file) {
  ...handle the file copy here, but don't call `res.redirect()` anywhere...

form.on("end", function() {

FWIW, it seems to me that you want to set the uploadDir option of Formidable, or use fs.rename(), instead of reading the file after upload and writing it to a new location (which isn't very efficient).