Request to Express endpoint won’t stop loading, even if I set response status (Multer and Express issues)

I’m on the process of making a filter based off evaluating a file’s signature obtained via a file’s buffer provided by Multer. I know Multer provides the MIME type, but I also have to check wether the file is actually of that MIME type via some other ways of validation.

Because of that, my steps are:

  1. Temporarily store the request’s files in Multer’s memoryStorage() to get the buffer, then
  2. Filter the valid files
  3. Append those valid files and other information I need for a newly made FormData
  4. Make a POST request to the other endpoint(“/upload”) with the new FormData
  5. Store files in the cloud with Multer

PROBLEM IS: Once I force some error on the second endpoint, the process ends up on a loop. The error is detected, however I cannot return any HTTP code as a response to the first endpoint!

First endpoint:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>/* NOTE: storeInMemory.array("files") stores in memory the files' buffer information, which
/* are needed in order to work with validating the files! */
app.post("/filesUpload", storeInMemory.array("files"), async (req, res) => {
const files = req.files;
/* this array stores the items i have to upload */
let uploadArray = [];
/* filter only the items i actually need to upload */
for (const item of files) {
/*-- some filter logic --*/
uploadArray.push(item);
}
/* create new FormData to store the actual files i have to upload -
/* as multer works with http a new formdata has to be created, so i can make a call to another endpoint */
let form = new FormData();
form.append("someData", req.body["someData"]);
/* append the files to the form i'll use to upload later */
uploadArray.forEach((item) => {
form.append("files", item.buffer, item.originalname);
});
try {
const postUrl = process.env.MY_URL + "/upload";
const myHeaders = { headers: { 'Content-Type': `multipart/form-data; boundary=${form._boundary}` } };
/* Request to other endpoint */
let uploadResult = await axios.post(postUrl, form, myHeaders);
if (uploadResult.status == 200) return res.send(uploadResult).status(200);
/* PROBLEM HERE: this is the point the program never gets to and i need it to */
else {
return res.send(uploadResult).status(500);
}
}
catch (error) {
console.error("Unpredicted error occurred: ", error);
return res.send(error)
}
});
</code>
<code>/* NOTE: storeInMemory.array("files") stores in memory the files' buffer information, which /* are needed in order to work with validating the files! */ app.post("/filesUpload", storeInMemory.array("files"), async (req, res) => { const files = req.files; /* this array stores the items i have to upload */ let uploadArray = []; /* filter only the items i actually need to upload */ for (const item of files) { /*-- some filter logic --*/ uploadArray.push(item); } /* create new FormData to store the actual files i have to upload - /* as multer works with http a new formdata has to be created, so i can make a call to another endpoint */ let form = new FormData(); form.append("someData", req.body["someData"]); /* append the files to the form i'll use to upload later */ uploadArray.forEach((item) => { form.append("files", item.buffer, item.originalname); }); try { const postUrl = process.env.MY_URL + "/upload"; const myHeaders = { headers: { 'Content-Type': `multipart/form-data; boundary=${form._boundary}` } }; /* Request to other endpoint */ let uploadResult = await axios.post(postUrl, form, myHeaders); if (uploadResult.status == 200) return res.send(uploadResult).status(200); /* PROBLEM HERE: this is the point the program never gets to and i need it to */ else { return res.send(uploadResult).status(500); } } catch (error) { console.error("Unpredicted error occurred: ", error); return res.send(error) } }); </code>
/* NOTE: storeInMemory.array("files") stores in memory the files' buffer information, which 
/* are needed in order to work with validating the files! */
app.post("/filesUpload", storeInMemory.array("files"), async (req, res) => {
    const files = req.files;

    /* this array stores the items i have to upload */
    let uploadArray = [];

    /* filter only the items i actually need to upload */
    for (const item of files) {
        /*-- some filter logic --*/
        uploadArray.push(item);
    }

    /* create new FormData to store the actual files i have to upload -
    /* as multer works with http a new formdata has to be created, so i can make a call to another endpoint */
    let form = new FormData();
    form.append("someData", req.body["someData"]);

    /* append the files to the form i'll use to upload later */
    uploadArray.forEach((item) => {
        form.append("files", item.buffer, item.originalname);
    });

    try {
        const postUrl = process.env.MY_URL + "/upload";
        const myHeaders = { headers: { 'Content-Type': `multipart/form-data; boundary=${form._boundary}` } };
        /* Request to other endpoint */
        let uploadResult = await axios.post(postUrl, form, myHeaders);

        if (uploadResult.status == 200) return res.send(uploadResult).status(200);
        /* PROBLEM HERE: this is the point the program never gets to and i need it to */
        else {
            return res.send(uploadResult).status(500);
        }
    }
    catch (error) {
        console.error("Unpredicted error occurred: ", error);
        return res.send(error)
    }

});

Second endpoint:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>app.post("/upload", async (req, res) => {
try {
await new Promise((resolve, reject) => {
/* upload the files to cloud */
uploadOnCloud.array("files")(req, res, (err) => {
if (err) {
console.error("Error happened successfully!", err)
return reject(err);
}
else
return resolve();
});
});
/* If no err detected, ends with 200 */
res.sendStatus(200);
}
catch (error) {
/* PROBLEM: can neither set status, nor anything else */
res.status(500).send(error.message);
}
});
</code>
<code>app.post("/upload", async (req, res) => { try { await new Promise((resolve, reject) => { /* upload the files to cloud */ uploadOnCloud.array("files")(req, res, (err) => { if (err) { console.error("Error happened successfully!", err) return reject(err); } else return resolve(); }); }); /* If no err detected, ends with 200 */ res.sendStatus(200); } catch (error) { /* PROBLEM: can neither set status, nor anything else */ res.status(500).send(error.message); } }); </code>
app.post("/upload", async (req, res) => {
    try {
        await new Promise((resolve, reject) => {
            /* upload the files to cloud */
            uploadOnCloud.array("files")(req, res, (err) => {
                if (err) {
                    console.error("Error happened successfully!", err)
                    return reject(err);
                }
                else
                    return resolve();
            });
        });
        /* If no err detected, ends with 200 */
        res.sendStatus(200);
    }
    catch (error) {
        /* PROBLEM: can neither set status, nor anything else */
        res.status(500).send(error.message);
    }
});

What could be wrong here?

New contributor

easy_to_feel_dumb is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật