HTML in a node app?

I want to make a discord bot, and i want it to be able to interact with a website. I put a simple test file under public/index.html and it just says OK on the webpage.

I looked at this post and i did not see anything that would help me.

My project’s name is meow-bot.

Hey there, @MeowCatPersonThing!

According to me, I think you have not ran the sendFile() function, perhaps, you just sent status 200 and logged that the route / has been pinged or visited. Your code may look like this:

const http = require('http');
const express = require('express');
const app = express();
app.get("/", (request, response) => {
  console.log(Date.now() + " Ping Received");
  response.sendStatus(200);
});
app.listen(process.env.PORT);
setInterval(() => {
  http.get(`http://${process.env.PROJECT_DOMAIN}.glitch.me/`);
}, 280000);

Thence, you’ll have to modify and add a few lines to send the public/index.html to the response.
To do the above task, follow the instructions down below:

  • Add two new line to the code that sends the file to the server. Once you’re done, your code will look like following:
const http = require('http');
const express = require('express');
const app = express();
// The express uses the directory named "public" as a static folder.
app.use(express.static('public'));

app.get("/", (request, response) => {
  console.log(Date.now() + " Ping Received");
  response.sendStatus(200);
  // The file "index.html" is being sent to the server, which will be rendered once someone has visited the route `/`
  response.sendFile(__dirname + '/public/index.html');
});
app.listen(process.env.PORT);

setInterval(() => {
  http.get(`http://${process.env.PROJECT_DOMAIN}.glitch.me/`);
}, 280000);

Hope this helps!

3 Likes

Thank you! It works now :tada:

2 Likes