A convenient helper module for node.js and request when you regularly work with an API in your projects.
I tend to write node.js applications that rely heavily on a separate API layer (regardless of if thats written in node itself or not). Because of this I tend to use the same convention when consuming those API endpoints. Over time I’ve written a fairly handy helper module that makes working with API endpoints a breeze!
The code itself is hosted over at GitHub which you can find here: https://github.com/jkat98/api_helper
Heres the example code from the repo on how to work with it:
[code lang=”javascript”]
var api = require(‘../api_helper’),
config = require(‘./config.json’);
// call one-time during server bootstrap:
api.configure(config);
// call anytime/anywhere:
api.callAPI(‘users/list’, ‘GET’, {}, function(err, body) {
if (!err) {
console.log(body); // => data returned from API endpoint!
}
});
[/code]
Where config.json contains the following configuration settings for the API itself:
[code lang=”javascript”]
{
“api”: {
“scheme”: “http”,
“host”: “api.somedomain.com”,
“port”: “8080”,
“version”: “v1”
}
}
[/code]
Now, whenever I want to consume an API endpoint in my project I simply use this helper module and it makes my life so much easier!