Search code examples
javascriptangularjsnode.jsexpressng-file-upload

File upload not working on localhost express server


I'm trying to implement ng file upload in a node app I'm running on localhost. I'm going off the demo here but when i change the directory to download to

file.upload = Upload.upload({
                url: 'uploadImages',
                data: {file: file}
            });

I'm getting a 404:

angular.js:10765 POST http://localhost:8888/uploadImages/ 404 (Not Found)

Do I need to set up an express route for that directory? I've attempted that, but it isn't working either with

app.post('/uploadImages', cors(corsOptions), function(req, res){
    res.sendfile('./uploadImages')
});

Not really sure where to go from here.


Solution

  • Yes, you need to set up a web server like your Node Express server to accept the POST request. The way I have done this in the past was to use multer, an Express middleware for handling multi-part uploads.

    EXAMPLE

    var express = require('express')
    var multer = require('multer')
    
    var app = express();
    
    var upload = multer({
        dest: 'uploadImages/'
    });
    
    app.post('/uploadImages', upload.any(), function (req, res, next) {
      // req.files is the file uploaded, which multer will write to
      // the dest folder for you. req.body will contain the text fields,
      // if there were any.
    
      res.json(req.files.file);
    });