Report this

What is the reason for this report?

I can't upload files from html form on my node/express app

Posted on July 4, 2017

Hello,

I’m just running a simple Node.js/Express application on a droplet and I can’t upload files to the designated folder from a html form on a page of the application.

I’m just running the application with ‘node app.js’ and it runs on port 3000. I could be wrong but I don’t think Apache is involved here, is that correct?

I’ve chmod 777’d the folder to where the files go but every time I try and upload a file the application hangs for a few seconds and then gets “Killed”.

The upload location is ‘app_root/public/uploads’

I am aware of the security issues here, I just want to do a little bit of live testing, I don’t plan to the have the application up for long.

Thanks for any help! Jonathan



This textbox defaults to using Markdown to format your answer.

You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!

These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.

Ok, It’s actually Jimp that is cause the problem here. I was trying to resize the file before saving, and it’s failing somewhere here:

function savePhoto(photo, userId) {
    var timestamp = Math.round((new Date()).getTime() / 1000);
    var photoname = userId + - + timestamp + '.png';
    jimp.read(photo.data, function (err, photo) {
        if (err) throw err;
        photo.resize(400, jimp.AUTO)            
            .quality(70)
            .write("public/uploads/" + photoname);
    });
    return photoname;
}

whereas if i use ‘express-fileupload’ to save the file, it works fine:

function savePhoto(photo, userId) {
    var timestamp = Math.round((new Date()).getTime() / 1000);
    var photoname = userId + - + timestamp + '.png';
    photo.mv('public/uploads/' + photoname, function(err) {
        if (err) throw err;
    });
    return photoname;
}

Thanks for any help!

have you included this line your express app setup?

app.use(express.static('public'));

Actually you may need to set up a reverse proxy like nginx which directs all the requests to your node app.

Check this link out. https://www.digitalocean.com/community/tutorials/how-to-set-up-a-node-js-application-for-production-on-ubuntu-16-04#set-up-nginx-as-a-reverse-proxy-server

The developer cloud

Scale up as you grow — whether you're running one virtual machine or ten thousand.

Get started for free

Sign up and get $200 in credit for your first 60 days with DigitalOcean.*

*This promotional offer applies to new accounts only.