Front end does not show when showing node.js server

Viewed 215

running my node server just returns a blank page, and not the front end. Can someone provide some advice?

front end script tag

async function getSample() {
const res = await fetch('/');
const data = await res.text();
  console.log(data);
}
  document.getElementById('button').addEventListener
   ('click', getSample);

  async function getSample() {
    fetch('/')
    .then(response => response.text())
    .then(data => console.log(data));
}

back end script tag:

const express = require('express')
const app = require ('express')();

const port = 3000

app.use(express.static("Public"));
  

app.get('/', (req, res) => {
    res.json()
})

app.listen(port, () => {
    console.log(`Example app listening at http://localhost:${port}`)
  })

Any insight would be greatly appreciated as I am still learning

1 Answers

The problem is with the / in the backend. You need to have an index.html in your project for it to render in the / of your frontend, but since it is also an API route, the browser will not be able to understand it as the front page of the website.

Related