Skip to content

Instantly share code, notes, and snippets.

@svnlto
Created August 27, 2016 10:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save svnlto/31316bd60f15f03cc032e4c6ba6bf357 to your computer and use it in GitHub Desktop.
Save svnlto/31316bd60f15f03cc032e4c6ba6bf357 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