Skip to content

Instantly share code, notes, and snippets.

@macedigital
Created September 18, 2015 13:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save macedigital/6b71b9547dd4421d8724 to your computer and use it in GitHub Desktop.
Save macedigital/6b71b9547dd4421d8724 to your computer and use it in GitHub Desktop.
Minimal example for using express-body-parser middleware
var express = require('express');
var app = express();
var http = require('http');
var server = http.createServer(app);
var xmlparser = require('express-xml-bodyparser');
// fyi: it'd be better to attach the parser to wanted routes only
app.use(xmlparser());
app.post('/xml', function(req, res, next) {
// req.body is an object, so we'll check if it has any (enumerable) properties
if (Object.keys(req.body).length) {
console.log('Parsed XML', req.body);
res.send('OK!');
} else {
res.send('Not OK :(');
}
console.log('post body length', req.rawBody.length); // this is a string
return res.end();
});
server.listen(1337);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment