Code Monkey home page Code Monkey logo

Comments (4)

fiznool avatar fiznool commented on May 30, 2024 1

@macedigital I think mirroring the original express-body-parser options, with an extra xmlOptions property in the object would be best.

{
  limit: '1MB',
  xmlOptions: {
    async: true,
    // etc
  }
}

Regarding a PR, I've decided not to use this module after all, it turns out that XML parsing with body-parser is actually pretty easy. I've included a code snippet below in case this is useful for anybody.

Regardless, thanks @macedigital for this module!

var express = require('express'),
    typeis = require('type-is');

var app = express();

var xmlParseOptions = {
  async: false,   // Setting to `true` causes issues with exceptions
  explicitArray: false,
  trim: true,
  normalize: true,
  normalizeTags: true,
  mergeAttrs: true,
  charkey: 'value',
  attrNameProcessors: [function(attr) {
    return '@' + attr;
  }]
};

app.use(bodyParser.text({
  type: '*/xml',
  limit: '1MB'
}));

app.use(function(req, res, next) {
  if(!typeis.hasBody(req) || !typeis(req, '*.xml')) {
    return next();
  }
   // Parse as XML
  var parser = new xml2js.Parser(xmlParseOptions);
  parser.parseString(req.body, function(err, xml) {
    if(err) {
      err.status = 400;
      return next(err);
    }
    req.body = xml || req.body;
    next();
  });
});

from express-xml-bodyparser.

macedigital avatar macedigital commented on May 30, 2024

Yes, raw-body integration is planned (soon-ish).

The only thing I'm not 100% sure of is how the option passing should work:

  • use a flat-map, pass raw-body and xml2js options in the same config object
  • use a separate key for passing options for raw-body
  • use a second argument exclusively for raw-body options

Actually, option 3 looks like the cleanest solution to me, as it preserves the established api and will never cause issues if either dependency would introduce an option with the same name.

@fiznool let me know what you think. 👍 if you would provide a PR.

from express-xml-bodyparser.

macedigital avatar macedigital commented on May 30, 2024

I created a gist https://gist.github.com/macedigital/3799eec3c25a29692a25ee021457b171, so this alternative solution doesn't get lost. If time permits, support for limiting body size will arrive soon.

from express-xml-bodyparser.

fiznool avatar fiznool commented on May 30, 2024

Just to mention that I published body-parser-xml a while ago, which fully supports the payload size limiting option, along with every other option that the mainbody-parser package supports.

from express-xml-bodyparser.

Related Issues (17)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.