Last active
October 30, 2017 21:23
-
-
Save monkbroc/b8eefe5f37bec032d4e408afbc484d9d to your computer and use it in GitHub Desktop.
Webhook server
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* Simple webserver that logs all requests to /hook to a file | |
* Usage: | |
* npm install express | |
* node server.js | |
*/ | |
var express = require('express'); | |
var fs = require('fs'); | |
var app = express(); | |
var logfile = 'webhook.log'; | |
fs.writeFileSync(logfile, '', { encoding: 'utf8' }); | |
app.get('/', function (req, res) { | |
res.set('content-type', 'text/plain'); | |
res.send( | |
'Webhook requests\n\n' + | |
fs.readFileSync(logfile, { encoding: 'utf8' }) | |
); | |
}); | |
app.all('/hook', function (req, res) { | |
console.log("Received hook"); | |
var body = ''; | |
req.setEncoding('utf8'); | |
req.on('data', function (chunk) { body += chunk; }); | |
req.on('end', function () { | |
var data = { | |
time: (new Date()).toISOString(), | |
method: req.method, | |
headers: req.headers, | |
params: req.params, | |
query: req.query, | |
body: body | |
}; | |
fs.appendFileSync(logfile, JSON.stringify(data, true, 2) + '\n', { encoding: 'utf8' }) | |
res.send('{ "status": "OK" }'); | |
}); | |
}); | |
var port = process.env.PORT || 3000; | |
app.listen(port, function () { | |
console.log('Listening on port ' + port); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment