0

My Angularjs app sends to Node.js-server an XML-string as POST-data.

var xmlString = (new XMLSerializer()).serializeToString(xmlData);
var fd = new FormData();
fd.append('xml', xmlString);
$http.post("/saveXML", fd, {
    transformRequest: angular.identity,
    headers: {'Content-Type': undefined}
}).success(function (response) {
    console.log('xml uploaded!!', response);
}).error(function (error) {
    console.log("Error while uploading the xml!");
});

and Node.js receives the data and writes it to a file.

app.post('/saveXML', function (request, response) {
    var xmlData = request.body.xml;
    console.log(request.body);
    fs.writeFile("./uploads/mergedXml.xml", xmlData, function(wError){
        if (wError) {
            console.log(wError.message);
            response.send({
                success: false,
                message: "Error! File not saved!"

            });
            throw wError;
        }
        console.log("success");
        response.send({
            success: true,
            message: "File successfully saved!"
        });
    });
});

The problem is that if the sent XML-string (POST xml data) is larger than 1MB, it is truncated by Node.js(?) to 1MB. So that "mergedXml.xml" is then 1MB or exact 1024 Kb.

I use for Node.js: "express", "fs", "multer", "body-parser".

I've tried various settings for this, for example:

app.use(multer({
    dest: './uploads/',
    limits: {
        fileSize: 999999999
    }
}));

app.use(bodyParser.raw({limit:  '10mb'}));

But they have not worked. What could be wrong here? Is this perhaps problem with angularjs POST method? I would be thankful for any help.

ERG
  • 111
  • 2
  • 9
  • 1
    Check if [this](http://stackoverflow.com/questions/19917401/node-js-express-request-entity-too-large) helps, do you see any errors in the console, where you run the node.js app? To exclude angular.js, you can try to post the file with curl or something similar. – Boris Serebrov Jan 26 '16 at 23:03
  • @BorisSerebrov Thanks. No, there are no error messages. Whatever that's a good idea to exclude a part form POST connection. I'll try it – ERG Jan 27 '16 at 11:27
  • Please post all your relevant code. Also, are you using any proxy server or load-balancer in between? – Shimon Brandsdorfer Aug 19 '19 at 21:10
  • Hm, can you try to set a specific content-type on the request and also specify the same content type in `app.use(bodyParser.raw({limit: '10mb'}))`? – Vsevolod Goloviznin Aug 20 '19 at 04:17
  • @ERG apart from setting `app.use(bodyParser.raw({limit: '100mb'})); ` note that these limit also depends on where your app is running . In my case the app was running on kuberneties and i had to configure at the Nginx to allow `client_max_body_size` to your need . Also i had different layers along the AWS cloud service that i had to config for the similarly – Joel Joseph Aug 23 '19 at 06:16
  • we are getting the same issue on our nodejs application. so we have added limit to 100mb but some times on chrome / firefox we are getting 413 req entity too large response. but on same system from safari with out issue we are able to browse application. Actually our application is running with docker image on ubuntu. can i make any changes here ? @JoelJoseph – murali krishna Jan 21 '20 at 11:06

1 Answers1

5

You can use require('body-parser-xml') module and set some configuration.

Please fine the below code snippet:

var express = require('express'); 
var bodyParser = require('body-parser');
require('body-parser-xml')(bodyParser);

var app = express();

app.use(bodyParser.json());
app.use(bodyParser.xml({
    limit: '10MB', // Reject payload bigger than 10 MB 
    xmlParseOptions: {
        normalize: true, // Trim whitespace inside text nodes 
        normalizeTags: false, // Transform tags to lowercase 
        explicitArray: false // Only put nodes in array if >1 
    }
}));

app.post('/saveXML', function (request, response) {
    var xmlData = request.body.xml;
    response.send(request.body);
});

var server = app.listen(3000);

I've verified with 1.9 mb payload its working fine, you can check at your end.

Ravi
  • 2,350
  • 1
  • 13
  • 11