Skip to content

Instantly share code, notes, and snippets.

@vemuruadi
Created July 26, 2016 11:51
Show Gist options
  • Save vemuruadi/7607d59cb3eb91c726a4cfa5d0d9c0b4 to your computer and use it in GitHub Desktop.
Save vemuruadi/7607d59cb3eb91c726a4cfa5d0d9c0b4 to your computer and use it in GitHub Desktop.
Webhook receiver with hapi.js
/*
Webhook receivers should accept a request and immediately respond with HTTP 204 No Content before processing the request. Here's how to do this with hapi.js.
Start this server:
`node webhooks-with-hapi.js`
Then make a request:
`curl http://localhost:8000/webhook-receiver -v`
Note the correct behavior: HTTP response will be sent and connection closed before the webhook processing starts.
*/
// Using hapi ^11.0.0
var Hapi = require('hapi');
// Create a server with a host and port
var server = new Hapi.Server();
server.connection({
host: 'localhost',
port: 8000
});
// Add the route to receive a webhook request
server.route({
method: 'GET',
path:'/webhook-receiver',
handler: function (request, reply) {
reply().code(204);
setImmediate(processRequest);
}
});
// Function to do busy work
function processRequest() {
console.log('connection should have closed');
console.log('processing start');
for (var i = 0; i < 5000000000; i++) {}
console.log('processing done');
}
// Start the server
server.start(function () {
console.log('Server running at:', server.info.uri);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment