【问题标题】:How to use react within Express?如何在 Express 中使用 react?
【发布时间】:2018-01-03 18:09:05
【问题描述】:

我有一个用 expressjs/nodejs 开发的网站。我想向它添加一个 reactjs 模块。我在 expressjs 应用程序的“客户端/构建”中添加了一个 reactjs 应用程序的构建。 'client' 被声明为静态:

app.use(express.static(path.join(__dirname, 'client')));

我希望http://localhost:3002/build/ 可以工作,但它没有。它显示一个没有错误的空白页面。开发者工具中的网络选项卡显示引用的 CSS 和 JS 文件已被获取,但没有显示任何反应组件。我不明白为什么会这样。

更新:

文件夹结构:

index.html:

<!DOCTYPE html>
<html lang="en">
   <head>
      <meta charset="utf-8">
      <meta name="viewport" content="width=device-width,initial-scale=1,shrink-to-fit=no">
      <meta name="theme-color" content="#000000">
      <link rel="manifest" href="/build/manifest.json">
      <link rel="shortcut icon" href="/build/favicon.ico">
      <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/latest/css/bootstrap.min.css">
      <title>React App</title>
      <link href="/build/static/css/main.dd5f9b05.css" rel="stylesheet">
   </head>
   <body>
      <noscript>You need to enable JavaScript to run this app.</noscript>
      <div id="root"></div>
      <script type="text/javascript" src="/build/static/js/main.971e2482.js"></script>
   </body>
</html>

【问题讨论】:

  • 您是否在构建中响应捆绑到一个 js 文件中的应用程序代码?如果是,引用捆绑的 javascript 的页面在哪里?指向 build 不会自动加载文件,除非它被称为 indext.html。
  • @BlessYahu 我理解你的意思,但这不是问题。 index.html 正在正确加载,这就是获取 JS 和 CSS 的原因。
  • 请投反对票的人解释一下投反对票的原因?

标签: node.js reactjs express


【解决方案1】:
app.use('/build', express.static(path.join(`${__dirname}/client`)));

我认为您必须先指定路径?

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-04-11
    • 1970-01-01
    • 2018-03-04
    • 2020-11-17
    • 1970-01-01
    • 2020-06-25
    • 1970-01-01
    相关资源
    最近更新 更多