Question

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

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


Submit an answer


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!

Sign In or Sign Up to Answer

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.

Accepted Answer

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

Become a contributor for community

Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.

DigitalOcean Documentation

Full documentation for every DigitalOcean product.

Resources for startups and SMBs

The Wave has everything you need to know about building a business, from raising funding to marketing your product.

Get our newsletter

Stay up to date by signing up for DigitalOcean’s Infrastructure as a Newsletter.

New accounts only. By submitting your email you agree to our Privacy Policy

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.