Multer: Multer silent crash on uploading

Created on 17 Apr 2017  Â·  10Comments  Â·  Source: expressjs/multer

When trying to upload a file of 143KB I receive the following error:

Bad Gateway

The proxy server received an invalid response from an upstream server.
Apache/2.4.10 (Debian) Server at 50.116.34.26 Port 80

When trying to upload a file of 4KB I receive one of the two, randomly:

Proxy Error

The proxy server received an invalid response from an upstream server.
The proxy server could not handle the request POST /grimoireworks/admin/uploads/uploadimage.

Reason: Error reading from remote server

or

Service Unavailable

The server is temporarily unable to service your request due to maintenance downtime or capacity problems. Please try again later.
Apache/2.4.10 (Debian) Server at 50.116.34.26 Port 80

this is the code I'm using on the route:

app.post('/admin/uploads/uploadimage',
    ajaxAdminLoggedIn,
    upload.single('image_file'),
    function(req, res, next) {
        console.log("start of upload");

        req.socket.setTimeout(10000);

        var supportedTypes = ['image/jpeg', 'image/png', 'image/gif'];
        var type = req.file.mimetype;//mime.lookup(req.files.image_file.path);

        console.log("before supportedTypes check");

        if (supportedTypes.indexOf(type) == -1) {
            fs.unlink(req.file.path);
            return res.send(415, 'Supported image formats: jpeg, jpg, jpe, gif, png.');
        }

        console.log("before targetPath");

        var targetPath = __dirname + '/public/images/';
        if (req.body.image_type == "wizard") {
            targetPath += 'classes/';
        } else if (req.body.image_type == "spell") {
            targetPath += 'spells/';
        } else {
            fs.unlink(req.file.path);
            return res.send(415, 'Invalid image type.');
        }

        console.log("before tempPath");

        var tempPath = req.file.path;

        console.log("before createReadStream");

        var source = fs.createReadStream(tempPath);
        var dest = fs.createWriteStream(targetPath + req.file.originalname);

        console.log(tempPath);
        console.log(targetPath + req.file.originalname);

        source.pipe(dest);

        console.log("after pipe");

        source.on('error', function(err) {
            if (err) {
                return res.send(500, 'Something went wrong');
            }
        });

        source.on('end', function() {
            //delete file from temp folder
            fs.unlink(tempPath, function(err) {
                if (err) {
                    return res.send(500, 'Something went wrong');
                }

                res.redirect('back');
            }); //#end - unlink
        }); //#end - on.end
    }
);

This is the form

<form action="<%= relativePath %>/admin/uploads/uploadimage" method="post" enctype="multipart/form-data">
                            <div class="form-group">
                                <p>Select the type of the image:</p>
                                <label>
                                    <input type="radio" name="image_type" id="image_type_wizard" value="wizard" checked>
                                    Wizard
                                </label>
                                <br>
                                <label>
                                    <input type="radio" name="image_type" id="image_type_spell" value="spell">
                                    Spell
                                </label>
                            </div>
                            <div class="form-group">
                                <label for="image_file">Select your image:</label>
                                <input type="file" name="image_file" id="image_file" />
                                <p class="help-block">Supported files: jpg, jpeg, gif, png.</p>
                            </div>
                            <div class="text-right">
                                <button type="submit" class="btn btn-success">Submit</button>
                            </div>
                        </form>

Besides this multer is just using this:

var bodyParser = require( 'body-parser' );
var multer  = require('multer');

No logs appear anywhere, the node application restarts when trying to upload a file.

Apache just shows:

[Sun Apr 16 22:40:11.299511 2017] [proxy_http:error] [pid 29899] (20014)Internal error: [client 189.35.245.253:7512] AH01102: error reading status line from remote server 127.0.0.1:6161, referer: http://50.116.34.26/grimoireworks/admin/uploads
[Sun Apr 16 22:40:11.527747 2017] [proxy:error] [pid 29900] (111)Connection refused: AH00957: HTTP: attempt to connect to 127.0.0.1:6161 (127.0.0.1) failed
[Sun Apr 16 22:40:11.527800 2017] [proxy:error] [pid 29900] AH00959: ap_proxy_connect_backend disabling worker for (127.0.0.1) for 60s
[Sun Apr 16 22:40:11.527806 2017] [proxy_http:error] [pid 29900] [client 189.35.245.253:7519] AH01114: HTTP: failed to make connection to backend: 127.0.0.1, referer: http://50.116.34.26/grimoireworks/admin/uploads

Most helpful comment

For the record, turns out, there was no crash :) PM2 watched the changes in the folder and rebuild the application automatically - looks like crash restarting without error log.
Excluding the download folder from the list of the watch (pm2 ... --ignore-watch "folder_name") stoped restarting.

All 10 comments

Currently debugging the same thing. Thought it had something to do with cors initially, but seeing the same failure when deployed on AWS Elastic Beanstalk with and without cors.

I too have the same thing, using nginx proxy and getting a 502 error each time.

EDIT: Nope, nothing to do with multer. I was getting an error elsewhere and PM2 was restarting the app without recording the error, thus making it look like a silent failure.

PM2 was restarting the app without recording the error, thus making it look like a silent failure.

Same problem..

For the record, my problem was in my nginx config! Not an issue with express for me. Maybe something you can check in Apache config?

For the record, turns out, there was no crash :) PM2 watched the changes in the folder and rebuild the application automatically - looks like crash restarting without error log.
Excluding the download folder from the list of the watch (pm2 ... --ignore-watch "folder_name") stoped restarting.

It seems like this is quite common, and it has happened to me as well. Be sure to take care when using nodemon, pm2, etc.

@Saishy feel free to reopen if you think that the problem was not nodemon/pm2/etc.

@iLyxa3D Thank you so much! Been digging around to resolve this issue for a while. So many technologies.. so many different possiblities.

But in the end it was all from watch.

Thank you again!

@sahandnayebaziz Got same error, only when running multer behind nginx revers proxy. Can you please share what was the reason for you?

Multer stopped working when I added an nginx reverse proxy. Can you please share your nginx configuration?

@iLyxa3D
Big thanks! The fix was so simple… I thought the issue was caused by nginx but not PM2.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

samipjain picture samipjain  Â·  4Comments

kiwenlau picture kiwenlau  Â·  4Comments

ChristianRich picture ChristianRich  Â·  4Comments

tjabdoullah picture tjabdoullah  Â·  4Comments

Paul-Morris picture Paul-Morris  Â·  3Comments