Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
269 views
in Technique[技术] by (71.8m points)

javascript - Front end does not show when showing node.js server

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...