Skip to content

Commit 531ae94

Browse files
committed
Added README file
1 parent 9d9c492 commit 531ae94

File tree

1 file changed

+103
-0
lines changed

1 file changed

+103
-0
lines changed

README.md

Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
# XML Body Parser
2+
3+
Adds XML parsing to the [body-parser](https://github.com/expressjs/body-parser) library, so you can convert incoming XML data into a JSON representation.
4+
5+
This is really useful if you want to deal with plain old JavaScript objects, but you need to interface with XML APIs.
6+
7+
## Installation
8+
9+
```
10+
npm install --save express body-parser body-parser-xml
11+
```
12+
13+
## Usage
14+
15+
This library adds an `xml` method to the `body-parser` object.
16+
17+
Initialise like so:
18+
19+
``` js
20+
var bodyParser = require('body-parser');
21+
require('body-parser-xml')(bodyParser);
22+
```
23+
24+
Once initialised, you can use it just like any other `body-parser` middleware:
25+
26+
``` js
27+
var app = require('express')();
28+
app.use(bodyParser.xml());
29+
```
30+
31+
This will parse any XML-based request and place it as a JavaScript object on `req.body` for your route handlers to use.
32+
33+
An XML-based request is determined by the value of the `Content-Type` header. If this ends in `xml`, it will be parsed as XML. For example, the following Content-Types will all match:
34+
35+
- `text/xml`
36+
- `application/xml`
37+
- `application/rss+xml`
38+
39+
You can also pass in options:
40+
41+
``` js
42+
app.use(bodyParser.xml(options));
43+
```
44+
45+
The `options` object accepts any of the following keys:
46+
47+
### defaultCharset
48+
49+
Specify the default character set for the text content if the charset is not specified in the `Content-Type` header of the request. Defaults to `utf-8`.
50+
51+
### inflate
52+
53+
When set to `true`, then deflated (compressed) bodies will be inflated; when `false`, deflated bodies are rejected. Defaults to `true`.
54+
55+
### limit
56+
57+
Controls the maximum request body size. If this is a number, then the value specifies the number of bytes; if it is a string, the value is passed to the [bytes](https://www.npmjs.com/package/bytes) library for parsing. Defaults to `'100kb'`.
58+
59+
### verify
60+
61+
The `verify` option, if supplied, is called as `verify(req, res, buf, encoding)`, where `buf` is a `Buffer` of the raw request body and `encoding` is the encoding of the request. The parsing can be aborted by throwing an error.
62+
63+
### xmlParseOptions
64+
65+
This option controls the behaviour of the XML parser. You can pass any option that is supported by the [xml2js](https://github.com/Leonidas-from-XIV/node-xml2js) library: [see here](https://github.com/Leonidas-from-XIV/node-xml2js#options) for a list of these options.
66+
67+
## Example
68+
69+
``` js
70+
var express = require('express'),
71+
bodyParser = require('body-parser');
72+
73+
require('body-parser-xml')(bodyParser);
74+
75+
var app = express();
76+
app.use(bodyParser.xml({
77+
limit: '1MB', // Reject payload bigger than 1 MB
78+
xmlParseOptions: {
79+
normalize: true, // Trim whitespace inside text nodes
80+
normalizeTags: true, // Transform tags to lowercase
81+
explicitArray: false // Only put nodes in array if >1
82+
}
83+
}));
84+
85+
app.post('/users', function(req, res, body) {
86+
// Any request with an XML payload will be parsed
87+
// and a JavaScript object produced on req.body
88+
// corresponding to the request payload.
89+
console.log(req.body);
90+
res.status(200).end();
91+
});
92+
93+
```
94+
95+
## Motivation
96+
97+
This library was born out of a frustration that [express-xml-bodyparser](https://github.com/macedigital/express-xml-bodyparser), the most popular XML-parsing library for express, doesn't support the regular `body-parser` options - in particular, limiting the payload size.
98+
99+
This library was written to use `body-parser`'s text parser under the hood, and then passes the parsed string into the XML parser. We can therefore take advantage of `body-parser`'s regular options, and support limiting the payload size, amongst other things.
100+
101+
## License
102+
103+
MIT

0 commit comments

Comments
 (0)