I am building a headless CMS with Strapi. I am testing the webhooks section and want to show the received data from the webhook on my React front-end.
I created a new folder webhooks
on my local machine and ran npm init -y
.
It created a package.json
file with this content in it:
{
"name": "webhooks",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
},
"keywords": [],
"author": "",
"license": "ISC",
"dependencies": {
"express": "^4.18.2"
}
}
I added a index.js
file to the root folder with this content and installed express
and body-parser
:
const express = require("express")
const bodyParser = require("body-parser")
const app = express()
const PORT = 3001
app.use(bodyParser.json())
app.post("/webhook", (req, res) => {
console.log(req.body)
res.status(200).end()
})
app.listen(PORT, () => console.log(` Server running on port ${PORT}`))
After that I added this line in my package.json
:
"start": "node index.js"
So it will start up with npm start
instead of node index.js
.
I added this URL to my strapi webhooks: http://localhost:3001/webhook
and tested the trigger from the Strapi admin. It works fine.
After this I ran npx create-react-app client
to create my react front-end app.
My next question is now how can I receive the contents from the webhook in my react front-end app?