Body parsing plugin for YAPS
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 

30 lines
912 B

var body = function(options){
this.settings = options || {};
this.settings.bodyAsJSON = this.settings.bodyAsJSON || false;
this.on("setup", function(request, server, done){
var self = this;
request.body = new Buffer(0);
request.on("data", function(chunk){
request.body = Buffer.concat([request.body, chunk]);
});
request.on("end", function(){
if(self.settings.bodyAsJSON){
try{
request.body = JSON.parse(request.body);
}catch(e){
request.body = undefined;
request.bodyError = e.toString();
}
}
done();
});
request.on("error", function(error){
request.body = undefined;
request.bodyError = error;
done();
});
});
};
module.exports = body;