NodeJS Webhooks

npm install node-webhooks --save 

// Initialize WebHooks.
var WebHooks = require('node-webhooks')
// Initialize webhooks from database
var webHooks = new WebHooks({
    db: './webHooksDB.json', // json file that store webhook URLs
    httpSuccessCodes: [200, 201, 202, 203, 204],
})
// Alternatively, initialize webhooks with object

webHooks = new WebHooks({
    db: {"addPost": ["http://localhost:9100/posts"]},
})
// sync instantation - add a new webhook called 'name1'
webHooks.add('name1', 'http://127.0.0.1:9000/prova/other_url').then(function(){
    // done
}).catch(function(err){
    console.log(err)
})
// add another webHook
webHooks.add('name2', 'http://127.0.0.1:9000/prova2/').then(function(){
    // done
}).catch(function(err){
    console.log(err)
});
// remove a single url attached to the given shortname
// webHooks.remove('name3', 'http://127.0.0.1:9000/query/').catch(function(err){console.error(err);})
// if no url is provided, remove all the urls attached to the given name
// webHooks.remove('name3').catch(function(err){console.error(err);})
// trigger a specific webHook
webHooks.trigger('name1', {data: 123})
webHooks.trigger('name2', {data: 123456}, {header: 'header'}) // payload will be sent as POST request with JSON body (Content-Type: application/json) and custom header

Share this

Related Posts

Previous
Next Post »

Pageviews from the past week