【问题标题】:json-server homepage is not showingjson-server 主页未显示
【发布时间】:2021-02-24 13:04:16
【问题描述】:

我在 package.json 中使用 npx create-react-app 创建了一个 react-app 添加了 json-server 并运行脚本。

"devDependencies": {
   "json-server": "0.14.2",
   "react-scripts": "1.0.10",
   "styled-components": "^2.1.1"
},

"scripts": {
    "apiserver": "json-server -p 3001 -w tools/db.json",

当我使用npm run apiserver 启动 json-server 时,它会成功启动。但是主页没有加载。

 \{^_^}/ hi!

  Loading tools/db.json
  Done

  Resources
  http://localhost:3001/posts

  Home
  http://localhost:3001

  Type s + enter at any time to create a snapshot of the database
  Watching...

没有加载:(

我还全局安装了 json-server。 所以当我这样开始时:

json-server -w db.json -p 3001

加载成功:

【问题讨论】:

    标签: reactjs npm package.json json-server


    【解决方案1】:

    这是由于您已经在应用程序中拥有public 目录,解决方法是here

    对我来说,我尝试传递公用文件夹的静态 URL,并且成功了。

    因此,对您而言,一个简单的解决方案是将apiserver 脚本更新为以下内容;

    "apiserver": "json-server -p 3001 -w tools/db.json -s ./node_modules/json-server/public"

    【讨论】:

    • apiserver 脚本在哪里?
    • 是package.json文件中的一段
    【解决方案2】:

    在项目根目录下创建 json-server.json 文件,并添加自定义配置:

    {
      ...,
      "static": "./node_modules/json-server/public"
    }
    

    通过此解决方案,您可以从应用中存在的静态非公共目录调用您的主路由。

    【讨论】:

      猜你喜欢
      • 2021-01-03
      • 2014-10-12
      • 1970-01-01
      • 2016-06-17
      • 2020-11-26
      • 1970-01-01
      • 1970-01-01
      • 2023-01-15
      • 2020-06-14
      相关资源
      最近更新 更多