Friday, 2 June 2017

Multer upload returning 404

I have a an AJAX upload form that posts to an Express server (/upload). The server is using Multer to handle the file upload. For some reason, the response always returns the error "Cannot GET /upload".

Client side code:

$.ajax({
    url: '/upload',
    method: 'POST',
    processData: false,
    contentType: false,
    data: formData
 });

Server side code:

var app = express();

var upload = multer({
                 dest: './upload'
             });

app.post('/upload', upload.single('file'), function(req, res) {
    return res.send({"message": "Success"});
});

app.use(express.static(path.join(__dirname, "public"));

The strange thing is if I debug and place a breakpoint inside the upload function, then it returns successfully, which leads me to believe there is some kind of race condition happening, but I cannot pinpoint it.

I am running the latest Node 6.x server, Express 4, Multer and Jquery libs.



via Choy

No comments:

Post a Comment