使用Node.js中的’get’请求时,React部分的HTML无法加载到网页上。

huangapple go评论78阅读模式
英文:

React part of HTML wont load onto webpage when using 'get' request in nodejs

问题

我正在学习Web开发,我正在使用Visual Studio Code。我使用命令"npx create-react-app my-app"创建了一个React应用程序。然后我进行了一些前端开发,并使用"npm start"命令运行了网站,一切正常。

在进行了一些前端开发之后,我决定为后端创建一个"server"文件夹。在这里,我尝试使用"app.get"将index.html文件加载到浏览器上,但是React部分没有加载到页面上。HTML文件被显示出来,但是应该显示在

中的所有React内容都没有显示出来。我该如何解决这个问题?

文件目录如下:

file directories

public文件夹中的index.html文件:

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <link
      href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.1/dist/css/bootstrap.min.css"
      rel="stylesheet"
      integrity="sha384-4bw+/aepP/YC94hEpVNVgiZdgIC5+VKNBQNGCHeKRQN+PtmoHDEXuppvnDJzQIu9"
      crossorigin="anonymous"
    />
    <title>Food Town</title>
    <link rel="shortcut icon" type="image/png" href="\Images\favicon.jpg" />
    <script
      src="https://kit.fontawesome.com/73800f79be.js"
      crossorigin="anonymous"
    ></script>
  </head>
  <body>
    Hello!!!
    <div id="root"></div>
    <script
      src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.1/dist/js/bootstrap.bundle.min.js"
      integrity="sha384-HwwvtgBNo3bZJJLYd8oVXjrBZt8cqVSpeBNS5n7C8IVInixGAoxmnlMuBnhbgrkm"
      crossorigin="anonymous"
    ></script>
  </body>
</html>

server文件夹中的index.js文件:

const express = require("express");
const bodyParser = require("body-parser");
const mongoose = require("mongoose");
const cors = require("cors");
const app = express();
const path=require("path");
const dotenv = require("dotenv").config();
const staticPath= path.join(__dirname, '../food-site');


app.use(cors());
app.use(express.urlencoded({ extended: true }));
app.use(express.static(staticPath));

app.get("/", (req,res)=>{
  res.sendFile(path.join(__dirname , '../food-site/public/index.html'));
});

const PORT = process.env.PORT;
app.listen(PORT, () => {
  console.log("Server connected on port: " + PORT);
});

起初,我遇到了必须使用绝对路径或相对路径的错误,HTML文件根本没有显示出来。我尝试在网上和YouTube上寻找可能的解决方案,并成功解决了这个问题。然而,React部分没有渲染到屏幕上,我找不到任何解决方案。

英文:

Im learning web development. Im using visual studio code
So I created a react app using "npx create-react-app my-app"
Then I did some frontend and ran the site using 'npm start' command, it was working fine

After working on some frotend, I decided to create a 'server' folder for the backend
Here i tried to load the index.html file onto the browser using 'app.get', however the react part dont not load onto the page. The html file gets displayed, but all the react stuff that should be coming into the
div id="root"
does not show up.
How do I fix this?

the file directories:

file directories

the index.html file inside the public folder:

&lt;!DOCTYPE html&gt;
&lt;html lang=&quot;en&quot;&gt;
  &lt;head&gt;
    &lt;meta charset=&quot;UTF-8&quot; /&gt;
    &lt;meta name=&quot;viewport&quot; content=&quot;width=device-width, initial-scale=1.0&quot; /&gt;
    &lt;link
      href=&quot;https://cdn.jsdelivr.net/npm/bootstrap@5.3.1/dist/css/bootstrap.min.css&quot;
      rel=&quot;stylesheet&quot;
      integrity=&quot;sha384-4bw+/aepP/YC94hEpVNVgiZdgIC5+VKNBQNGCHeKRQN+PtmoHDEXuppvnDJzQIu9&quot;
      crossorigin=&quot;anonymous&quot;
    /&gt;
    &lt;title&gt;Food Town&lt;/title&gt;
    &lt;link rel=&quot;shortcut icon&quot; type=&quot;image/png&quot; href=&quot;\Images\favicon.jpg&quot; /&gt;
    &lt;script
      src=&quot;https://kit.fontawesome.com/73800f79be.js&quot;
      crossorigin=&quot;anonymous&quot;
    &gt;&lt;/script&gt;
  &lt;/head&gt;
  &lt;body&gt;
    Hello!!!
    &lt;div id=&quot;root&quot;&gt;&lt;/div&gt;
    &lt;script
      src=&quot;https://cdn.jsdelivr.net/npm/bootstrap@5.3.1/dist/js/bootstrap.bundle.min.js&quot;
      integrity=&quot;sha384-HwwvtgBNo3bZJJLYd8oVXjrBZt8cqVSpeBNS5n7C8IVInixGAoxmnlMuBnhbgrkm&quot;
      crossorigin=&quot;anonymous&quot;
    &gt;&lt;/script&gt;
  &lt;/body&gt;
&lt;/html&gt;

the index.js file inside the server folder:

const express = require(&quot;express&quot;);
const bodyParser = require(&quot;body-parser&quot;);
const mongoose = require(&quot;mongoose&quot;);
const cors = require(&quot;cors&quot;);
const app = express();
const path=require(&quot;path&quot;);
const dotenv = require(&quot;dotenv&quot;).config();
const staticPath= path.join(__dirname, &#39;../food-site&#39;);


app.use(cors());
app.use(express.urlencoded({ extended: true }));
app.use(express.static(staticPath));

app.get(&quot;/&quot;, (req,res)=&gt;{
  res.sendFile(path.join(__dirname , &#39;../food-site/public/index.html&#39;));
});

const PORT = process.env.PORT;
app.listen(PORT, () =&gt; {
  console.log(&quot;Server connected on port: &quot; + PORT);
});

At first I was getting the error that I had to use absolute path or relative path, the html file wasnt displaying at all.
I tried looking online and on youtube for possible solutions and was able to resolve that issue.
However, the react portion wasnt rendering onto the screen and i wasnt able to find any solution for it.

答案1

得分: 0

你没有任何代码会查看 #root。所有的脚本都是与 React 无关的库。

看起来你把原始源代码放到了你的 Web 服务器上,而不是运行构建脚本(可能使用 webpack)并部署输出。

英文:

There's no code which would look anything into #root there. All the scripts are for libraries that have nothing to do with React.

It looks like you have dropped your raw source code into your web server instead of running your build script (which probably uses webpack) and deploying the output from it.

huangapple
  • 本文由 发表于 2023年8月8日 23:58:23
  • 转载请务必保留本文链接:https://go.coder-hub.com/76861283.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定