Skip to content

Instantly share code, notes, and snippets.

@afreeland
Last active August 7, 2020 13:08
Show Gist options
  • Save afreeland/ed4e4591adde87c952432828e45663cd to your computer and use it in GitHub Desktop.
Save afreeland/ed4e4591adde87c952432828e45663cd to your computer and use it in GitHub Desktop.
Webhook middleware (Chargify)
const app = express();
function rawBodySaver(req, res, buf, encoding) {
if (buf && buf.length) {
req.rawBody = buf.toString(encoding || "utf8");
}
}
// For parsing application/json
app.use(express.json());
// For parsing application/x-www-form-urlencoded (which is the Content-Type from Chargify)
// We can add a verify function that allows us access to the Buffer, which we
// can then expose it out on the request as `rawBody`
app.use(express.urlencoded({ verify: rawBodySaver, extended: true }));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment