Skip to content

Instantly share code, notes, and snippets.

@andy-polhill
Last active March 15, 2022 12:14
Show Gist options
  • Save andy-polhill/70b2c8e86a3bda700754928b4a05b883 to your computer and use it in GitHub Desktop.
Save andy-polhill/70b2c8e86a3bda700754928b4a05b883 to your computer and use it in GitHub Desktop.
A simple node server to test webhooks
  1. Run the server with node index.js
  2. Use ngrok to expose the server publicly ngrok http 3000
  3. Subscribe to the relevant hook.
mutation subscribeToHook {
  createdByAdminWebhookSubscriptionCreate(
    topic: <Webhook Topic>
    webhookSubscription: {
      apiVersion: "unstable"
      callbackUrl: "https://<NGROK URL>/hook"
    }
  ) {
    webhookSubscription {
      endpoint {
        __typename
      }
    }
  }
}
const express = require("express")
const bodyParser = require("body-parser")
const fs = require('fs');
const app = express()
app.use(bodyParser.json())
app.listen(3000, () => console.log(`🚀 Server running`))
// generate a file name for this server session
const now = new Date();
const filename = `./output-${now.getFullYear()}-${now.getMonth()}-${now.getDate()}-${now.getHours()}-${now.getMinutes()}`;
console.log(`writing output to: ${filename}`);
app.use(bodyParser.json())
const process = (req, res, suffix) => {
const fd = fs.openSync(`${filename}-${suffix}.json`, 'a');
fs.appendFileSync(fd, "\n\n\n\n// == NEW ENTRY ==\n\n")
fs.appendFileSync(fd, "\n\n\n\n// = HEADERS\n\n")
fs.appendFileSync(fd, JSON.stringify(req.headers, null, 2));
fs.appendFileSync(fd, "\n\n\n\n// = BODY\n\n")
fs.appendFileSync(fd, JSON.stringify(req.body, null, 2))
fs.closeSync(fd);
res.status(200).end()
}
app.post("/order", (req, res) => process(req, res, 'order'))
app.post("/fulfillment", (req, res) => process(req, res, 'fulfillment'))
app.post("/fulfillment-events", (req, res) => process(req, res, 'fulfillment-events'))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment