All Projects → macedigital → express-xml-bodyparser

macedigital / express-xml-bodyparser

Licence: MIT License
Simple XML body parser connect/express middleware

Programming Languages

javascript
184084 projects - #8 most used programming language

Projects that are alternatives of or similar to express-xml-bodyparser

redux-tools
Redux tools to speed up development.
Stars: ✭ 16 (-75%)
Mutual labels:  middleware
NiFi4Trading
NiFi Bundle for FIX Protocol
Stars: ✭ 14 (-78.12%)
Mutual labels:  middleware
horse-logger
Middleware for access logging in HORSE
Stars: ✭ 25 (-60.94%)
Mutual labels:  middleware
polix
🚀 Node.js Web Framework
Stars: ✭ 32 (-50%)
Mutual labels:  middleware
AkamaiOPEN-edgegrid-php-client
PHP client library for Akamai {OPEN} EdgeGrid Authentication scheme (based on Guzzle)
Stars: ✭ 38 (-40.62%)
Mutual labels:  middleware
horse-jwt
Middleware for JWT in HORSE
Stars: ✭ 39 (-39.06%)
Mutual labels:  middleware
react-redux-api-tools
A set of tools to facilitate react-redux development and decouple logic from compontents
Stars: ✭ 37 (-42.19%)
Mutual labels:  middleware
whoops-middleware
PSR-15 compatible middleware for Whoops, the pretty error handler
Stars: ✭ 24 (-62.5%)
Mutual labels:  middleware
oryx
.NET Cross platform and highly composable middleware for building web request handlers in F#
Stars: ✭ 178 (+178.13%)
Mutual labels:  middleware
spiderable-middleware
🤖 Prerendering for JavaScript powered websites. Great solution for PWAs (Progressive Web Apps), SPAs (Single Page Applications), and other websites based on top of front-end JavaScript frameworks
Stars: ✭ 29 (-54.69%)
Mutual labels:  middleware
middleland
Simple PSR-15 middleware dispatcher
Stars: ✭ 31 (-51.56%)
Mutual labels:  middleware
redux-global-loader
A Redux middleware for global loader
Stars: ✭ 13 (-79.69%)
Mutual labels:  middleware
xenon
A middleware abstraction library that provides a simple programming interface to various compute and storage resources.
Stars: ✭ 28 (-56.25%)
Mutual labels:  middleware
ReSwiftMonitor
ReSwift+redeux dev tools
Stars: ✭ 13 (-79.69%)
Mutual labels:  middleware
html2any
🌀 parse and convert html string to anything
Stars: ✭ 43 (-32.81%)
Mutual labels:  xml-parser
DevOps
DevOps code to deploy eScience services
Stars: ✭ 19 (-70.31%)
Mutual labels:  middleware
connect-browser-sync
Connect middleware for BrowserSync
Stars: ✭ 16 (-75%)
Mutual labels:  middleware
express-ping
Let all your express applications expose a common API to inform about their internal status and health.
Stars: ✭ 50 (-21.87%)
Mutual labels:  middleware
AspNetCore.Weixin
An ASP.NET Core middleware for Wechat/Weixin message handling and apis. (微信公众平台/接口调用服务)
Stars: ✭ 24 (-62.5%)
Mutual labels:  middleware
DotNETCarRental
Daily car rental simulation with ASP.NET.
Stars: ✭ 13 (-79.69%)
Mutual labels:  middleware

NPM Version Dependency Status Build Status Code Coverage status Gitter channel

express-xml-bodyparser

For those rare cases when you have to parse incoming raw xml-body requests. This middleware works with any connect- or express-based nodejs application.

Description

Admittedly, having to deal with XML data has become less common in recent years. Still, there are services and APIs using this format. The middleware is based on the connect-json middleware as a blueprint.

There is a similar xml bodyparser module available, but you might appreciate some notable differences:

  • Custom configuration options how to deal with XML data.
  • Attempt to parse data only once, even if middleware is called multiple times.
  • Skip data parsing immediately if no req-body has been sent.
  • Accept any XML-based content-type, e.g. application/rss+xml
  • No dependency on coffeescript keeping dependencies to a minimum.

Installation

Utilize npm by typing npm install express-xml-bodyparser --save in your projects root folder and your good to go.

Configuration

You can pass configuration options into the XML parser middleware. They're exactly the same options you would use for xml2js, which this middleware relies on. For further details look at all available configuration options.

Without specifying custom options, the middleware applies some opionated defaults meant to normalize the resulting json object properties. All whitespace in text nodes will be trimmed, property and tag names will be lowercased. The parser will always return node lists explicitly cast to Array.

NOTE: Custom options will be merged with aforementioned opionated defaults, so in case you want to use xml2js defaults, you will have to specify the following:

var xml2jsDefaults = {
    explicitArray: false,
    normalize: false,
    normalizeTags: false,
    trim: true
}

This change appeared in v0.1.0, older versions would merge options against xml2js's default options.

Typescript support

There are now type-definitions available at DefinitelyTyped. In order to use them in your project, add this to your (development) dependencies:

npm install --save-dev @types/express-xml-bodyparser

Thanks to @noticeMaker for creating the type-definitions.

Usage

You can either use express-xml-bodyparser at application level, or for specific routes only.

Here is an example of an express application with default settings:

var express = require('express'),
    app = express(),
    http = require('http'),
    server = http.createServer(app),
    xmlparser = require('express-xml-bodyparser');

// .. other middleware ...
app.use(express.json());
app.use(express.urlencoded());
app.use(xmlparser());
// ... other middleware ...

app.post('/receive-xml', function(req, res, next) {

  // req.body contains the parsed xml

});

server.listen(1337);

If you wanted to use express-xml-bodyparser for specific routes only, you would do something like this:

app.post('/receive-xml', xmlparser({trim: false, explicitArray: false}), function(req, res, next) {
  // check req.body
});

Above example demonstrates how to pass custom options to the XML parser.

Customize mime-type detection

If you want to customize the regular expression that checks whether the xmlparser should do its work or not, you can provide your own by overloading the xmlparser.regexp property, like so:

var xmlparser = require('express-xml-bodyparser');
xmlparser.regexp = /^text\/xml$/i;

Doing so, will allow you to restrict XML parsing to custom mime-types only. Thanks to @ophentis for the suggestion. Just make sure your regular expression actually matches mime-types you're interested in. The feature is available since version v0.0.5.

IMPORTANT In versions v0.2.x custom regular expressions were ignored in mime-type parsing. The issue has been fixed in v0.3.0. If you need/rely on this feature, please upgrade to a newer version. Many thanks to @dirksen who discovered this issue.

Roadmap to v1.0.0

Lets start a discussion how to get to there (stable API).

Here are some thoughts:

  • 100% align with body-parser's error and success handling when dealing with req.body and req.rawBody.
  • Possibly dropping support for connect altogether at one point?
  • Deprecate mutating the xmlparser middleware's mime-type regexp in favor of passing customizations into the options parameters (perfect if using route-middlewares).
  • Refactor to use node's StreamAPIv2 (in effect requiring nodejs >= v0.10.x).
  • Require raw-body with added benefits of limiting request body size and setting charset-encoding based on request data.
  • Finally really apply and automate conventional changelog practice :)
  • Provide functional tests incorporating (any~~ version of) express.~~
Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].