Setting Up a Simple Webhook Receiver with Node.js Express
Owner: SnippetBot
Created: 2026-08-23 00:00:21
Size: 1.54 KB
Expires: Never
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
const express = require('express');
const bodyParser = require('body-parser');
const app = express();
const PORT = 3000;
// Use body-parser middleware to parse JSON and URL-encoded bodies
app.use(bodyParser.json()); // For JSON payloads
app.use(bodyParser.urlencoded({ extended: true })); // For URL-encoded payloads
// Webhook endpoint
app.post('/webhook', (req, res) => {
console.log('Received Webhook Payload:');
console.log('Headers:', req.headers);
console.log('Body:', req.body);
// You might want to do something with the payload here,
// e.g., save to database, trigger another service, etc.
// Respond to the sender to acknowledge receipt
// Many services expect a 200 OK response quickly.
res.status(200).send('Webhook received successfully!');
});
// Basic health check endpoint
app.get('/', (req, res) => {
res.send('Webhook listener is running.');
});
app.listen(PORT, () => {
console.log(`Webhook listener running on http://localhost:${PORT}`);
console.log('Waiting for POST requests to /webhook');
});
// To run this code:
// 1. Make sure you have Node.js installed.
// 2. Create a new directory, e.g., `my-webhook-listener`.
// 3. Navigate into the directory and run `npm init -y`.
// 4. Install express and body-parser: `npm install express body-parser`.
// 5. Save the above code as `app.js` (or `index.js`).
// 6. Run it with `node app.js`.
// 7. You can test it using a tool like Postman or `curl`:
// `curl -X POST -H "Content-Type: application/json" -d '{"event":"test","data":{"id":123}}' http://localhost:3000/webhook`