node package manager
Stop writing boring code. Discover, share, and reuse within your team. Create a free org »

msgpack-response

msgpack-response

Build Status Dependency Status

An implementation of Message Pack middleware for ExpressJS.

Features

  • Automatic Message Pack detection (from the HTTP headers) and encoding of all JSON messages to Message Pack.
  • Extension of the current ExpressJS API; Introducing the Response.msgPack(jsObject) method on the standard ExpressJS Response object.

Getting Started

With auto-detection and transformation enabled, the middleware detects automatically the HTTP header Accept: application/x-msgpack and piggybacks the Response.json() method of the ExpressJS API, to encode the JSON response as Message Pack. This method is usefull, when you have existing applications that need use the middleware, without changing the codebase very much.

const msgpackResponse = require('msgpack-response');
 
app.use(msgpackResponse({auto_detect: true}));
 
app.get('/test_json', (req, res) {
    res.status(200).json({'messsage': 'a true test'});
})

Note: Remember the add the header Accept: application/x-msgpack in the request.

Also it can have auto detection and transformation disabled. The middleware extends the Response object of the ExpressJS framework, by adding the msgPack() method to it. Then to return an encoded response, you just use the Response.msgPack() method that accepts the Javascript object as parameter. For example,

const msgpackResponse = require('msgpack-response');
 
app.use(msgpackResponse({auto_detect: false}));
//or 
app.use(msgpackResponse());
 
app.get('/test_msgpack', (req, res) {
    res.status(200).msgPack({'messsage': 'a true test'});
});

Note: Intialize the middleware before the actual routes in the middleware chain to properly extend the Response Object.

Requirements

Node.js >= 6.0

Installation

With npm do:

npm install msgpack-response -save

About

This project is funded and maintained by Mobiltron, Inc.. We ❤️ open source software!

Check out our other open source projects or say 👋 on twitter @mobiltron.

Contribute

Contributions are welcome 🤘 We encourage developers like you to help us improve the projects we've shared with the community. Please see the Contributing Guide and the Code of Conduct.

Authors

See also the list of contributors who participated in this project.

License

msgpack-response is available under the MIT license. See the LICENSE file for more info.