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

July 4, 2017 298 views
Node.js Ubuntu 16.04

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

3 Answers
jonathannorris88 July 4, 2017
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

Node.js is an open source JavaScript runtime environment for easily building server-side and networking applications. Node.js applications can be run at the command line but this guide focuses on running them as a service using PM2, so that they will automatically restart on reboot or failure, and can safely be used in a production environment.
  • Is this in case the form request is not hitting the correct end point? Because I have a registration and login form that are finding their end points and working correctly. Would it be different with a file form?

    • what are you using for uploading the file? multer?
      try logging out the output to see if the endpoint is getting a request.

      • for the file uploading im using 'express-fileupload', which seems to work fine as i am getting the request to the endpoint, with the file contents, but then i try to resize the file and save it to the file system, this is where it stops and get killed.

        in this link you can see the last log that i am seeing before it dies:

        https://github.com/ThriceGood/Spots/blob/master/routes/index.js#L82

        which is called from the endpoint function here:

        https://github.com/ThriceGood/Spots/blob/master/routes/index.js#L40

        I am using Jimp to do the resize and save. As a test i am going to use the standard express-fileupload method for saving the file, just to see if its not just a problem with Jimps resizing function (which works fine locally).

      • wouldn't you know it! it seems that Jimp is the problem here for some reason. When I use express-fileupload to actually save the file to the file system it saves fine. But when Jimp tries it, it dies, don't know if its the resizing part or the file saving part. Oh well, will have to dig into that. Thanks for the help.

Have another answer? Share your knowledge.