Glitch Logs to Index.html

I will like to know how i can call glitch logs to my glitch index.html page.
This is my app.js code:

app.post(“/webhook”, (req, res) => {
let body = req.body;
console.log(JSON.stringify(req.body, null, 2));
https://abc.com/webhooks
if (req.body.object) {
if (
req.body.entry &&
req.body.entry[0].changes &&
req.body.entry[0].changes[0] &&
req.body.entry[0].changes[0].value.messages &&
req.body.entry[0].changes[0].value.messages[0]
) {
let phone_number_id =
let msg_body = req.body.entry[0].changes[0].value.messages[0].text.body;
axios({
method: “POST”,
url:
https://abc.com” +
id +
“/messages” ,
data: {
messaging_product: “path”,
to: from,
text: { body: "Ack: " + msg_body },
},
headers: { “Content-Type”: “application/json” },
});
}
res.sendStatus(200);
} else {
res.sendStatus(404);
}
});

Then i have this line to show my index,html page

app.get(“/”, function (request, response) {
response.sendFile(__dirname + ‘/views/index.html’);
});

The challenge i have is how to show the same logs on index,html page.

Well… If you want to get the terminal logs (server-side logs, if that’s what you mean by glitch logs), I think that’s not possible :frowning:

I mean server-side logs. Thanks

Well, I will need some time to understand your code :slight_smile:

1 Like

Wait, seems like your code has a bunch of errors, can you please make sure you pasted the code correctly? :thinking:

Edit: Oh sorry, I forgot to say something!

Click me

Welcome to the community!!!

1 Like

The code is working well because i can see my logs and every incoming notification on it but the challenge is how to get this on index page instead of having it on server-side logs. Thanks

Oh ok!

Server setup

  1. You create a variable, for example, “notificationList”
  2. Everytime you receive a notification, you add it to the variable

Then:

  1. Your index.html sends a request every, let’s say, 0.6 seconds to an endpoint defined by the server. The server returns a JSON of every notification according to the variable notificationList.
  2. Your index.html reads the JSON and shows it.

Done! I hope this helps :slight_smile:

Thanks so much, i now have the idea of how to get that done although i will be glad if simple sample that i can look up to achieve this. Just leaning but thank all the same for your time.

Thank you too! And welcome to the community! I will try to make a simple sample code ASAP

1 Like

Here’s your code:

Server

// Start your server.js with this:
var notifications = "";

// then add this when you recieve a notification:
notifications = notifications + "\n" + [NOTIFICATION_TEXT];

// Let's add an endpoint
app.post('/getNotifications', function(req, res){
 	 res.status(200).json({ notifications: notifications })
});

Your index.html code

<script>
setInterval(function () { // declare an interval
fetch('/getNotifications') /* Fetch the endpoint */
	.then(response => response.json()) // Make the response in JSON
	.then(data => function () {
data = data.notifications;
// do what you want with "data" now
});
}, 600); // Fetch the endpoint every 600 milliseconds
</script>

Thanks!

1 Like

I will do just that, am so much grateful for this.

You’re welcome!