Tuesday, 6 June 2017

Send data from formData to Express

I have some troubles to send some Form data to 'Express' (npm). Although it should be trivial, the 'req.body' remains undefined.. What am I doing wrong here?

[main.js]
document.getElementById('button').addEventListener('click', function(e) {
    var formData = new FormData();
    formData.append('foo', 'foo123');

    var url = "http://localhost:3000/upload";   
    var xhr = new XMLHttpRequest();
    xhr.open('POST', url, true);

    xhr.send(formData);
});

[routes/index.js] (Express)
var express = require('express');
var router = express.Router();

router.post('/upload', function(req, res) {
    res.send(req.body);     // req.body is undefined
});



via user13242344

No comments:

Post a Comment