javascript React为构建中的非默认路由呈现空白页面

nhaq1z21  于 2023-06-20  发布在  Java
关注(0)|答案(1)|浏览(124)
<Router>
      <Switch>
        {console.log("Loading Routes")}

        <Route path="/booking" component={Booking} />
        <Route path="/bookings" component={Bookings} />
        <Route path="/admin/login" component={AdminLogin} />
        <Route path="/admin/panel" component={AdminPanel} />
        <Route path="/admin/new" component={AddPhone} />
        <Route path="/admin/list" component={PhoneList} />
        <Route path="/admin/edit/:id" component={EditPhone} />
        <Route exact path="/" component={Home} />
        <Redirect to="/" />
      </Switch>
    </Router>
app.use(express.static("./client/build"));

// Routes for the Application

app.use("/auth", require("./routes/auth"));
app.use("/api", require("./routes/api"));
app.use("/api/booking", require("./routes/booking"));

app.get("/*", (req, res) => {
  console.log(1);
  res.setHeader("Content-type", "text/html");
  res.sendFile(path.join(__dirname, "client/build/index.html"));
});

app.get("/*/*", (req, res) => {
  console.log(1);
  res.setHeader("Content-type", "text/html");
  res.sendFile(path.join(__dirname, "client/build/index.html"));
});

app.listen(PORT, () => {
  console.log("Listening to PORT : ", PORT);
});

我使用上面的代码来服务它,在构建时一切都运行良好,但是当我用我的服务器服务文件时,默认的/ route运行得很好,但是/admin/login路由或任何其他路由加载一个空白页面。

nwo49xxi

nwo49xxi1#

您的服务器根目录是否包含.htaccess文件?
如果没有,创建.htaccess文件,并在其中:

<IfModule mod_rewrite.c>
 RewriteEngine On
 RewriteBase /
 RewriteRule ^index\.html$ - [L]
 RewriteCond %{REQUEST_FILENAME} !-f
 RewriteCond %{REQUEST_FILENAME} !-d
 RewriteCond %{REQUEST_FILENAME} !-l
 RewriteRule . /index.html [L]
</IfModule>

相关问题