Providing Message Pack Support to an ExpressJS Node App

Recently, the extended body-parser that added support for message pack by extending the standard body-parser was released in Mobiltron’s github page.

The module aims to replace the basic body-parser module, in order to decrease complexities for the app programmers. You just need to add the require to body-parser-with-msgpack. An example project is published in Github that exhibits the module’s usage.

var express = require('express');
var bodyParser = require('body-parser-with-msgpack');
var app = express();

// Enable mime-types: application/json, form-data and message pack!
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({extended: true}));
app.use(bodyParser.msgpack());

app.post('/data', function (req, res) {
  res.status(200).json({
  	message: 'success',
  	data: req.body
  });
});

app.listen(8080, function () {
  // Server started! [...]
});

The above example creates a POST route /data that accepts application/json, application/x-www-form-urlencoded, and application/x-msgpack mime types. The result is always returned as a Javascript object in req.body.

You can try it for yourself by cloning the Github repository.

Enjoy!

Leave a Reply

Your email address will not be published. Required fields are marked *