【问题标题】:Getting 404 error after running serve -l ./build after building react-app在构建 react-app 后运行 serve -l ./build 后出现 404 错误
【发布时间】:2021-03-15 09:11:46
【问题描述】:

当我尝试运行此命令时出现 404 错误

serve -l build

在构建了一个 react 应用之后,这是我的 ./app.js 和在文件中实现的 react-router。

import React from "react";
import Header from "./Components/Header";
import LeafletContainer from "./container/LeafletContainer";
import SignUp from "./Components/SignUp";
import Profile from "./Components/Profile";
import SavedLocations from "./Components/SavedLocations";
import Swiper from "./Components/swiper";

import { BrowserRouter as Router, Switch, Route } from "react-router-dom";

const Slash = () => {
  return (
    <>
      <Header />
      <LeafletContainer />
      <Swiper />
    </>
  );
};

function App() {
  return (
    <div className="wrapper">
      <Router>
        <Switch>
          <Route exact path="/" component={Slash} />
          <Route exact path="/signup" component={SignUp} />
          <Route exact path="/profile" component={Profile} />
          <Route exact path="/locations" component={SavedLocations} />
        </Switch>
      </Router>
    </div>
  );
}

export default App;

我也在 ./package.json 中定义了这一行

{
...,
    "homepage": "."
...,
}

【问题讨论】:

  • 你试过空字符串homepage吗?还是完全删除?请改用serve build
  • serve -l ./build 应该做什么?
  • 你为什么首先使用--listen标志?
  • @ChrisG 我已使用此推荐 bash serve -s build 解决了问题,感谢您的帮助

标签: javascript reactjs react-router react-router-dom serve


【解决方案1】:

使用 -l 标志,您应该提供一个应该监听的端点/端口。我想你应该在你的情况下使用 -s 标志:

serve -s build

或者您可以像这样将端口添加到您的命令中:

serve -l 3232 build

然后你就可以在 localhost:3232

上找到你的应用了

【讨论】:

    猜你喜欢
    • 2019-08-25
    • 1970-01-01
    • 2019-06-11
    • 1970-01-01
    • 2021-08-10
    • 2020-05-30
    • 2020-11-04
    • 2017-02-12
    • 2021-05-28
    相关资源
    最近更新 更多