简体   繁体   English

当我将 React 应用程序与 json-server 一起部署时,如何修复 heroku 上的应用程序错误

[英]how to fix application error on heroku when i deploy react app along with json-server

I have been trying out json server along with react but for some reason I keep getting application error on Heroku. is it alright to use json-server for testing purposes.i have shared my server.js, file structure and package.json我一直在尝试 json 服务器和 React,但由于某种原因,我在 Heroku 上不断收到应用程序错误。可以使用 json-server 进行测试吗?我已经共享了我的 server.js、文件结构和 package.json

server.js服务器.js

    const jsonServer = require('json-server');
    const app = jsonServer.create();
    const path = require('path');
    const express = require('express');
    // const middlewares = jsonServer.defaults();
    const router = jsonServer.router('db.json');
    const port = process.env.PORT || 3001;
    
    **app.use('/data', router);
    app.use(express.static(path.join(__dirname, 'build')));
    app.get('/*', function (req, res) {
        res.sendFile(path.join(__dirname, 'build', 'index.html'));
    });**
    
    server.listen(port);

my folder structure我的文件夹结构

----client
    ----node modules
    ----public
    ----src
    ----packgage.json
----db.json
----packgae.json
----server.js

package.json package.json

{
  "name": "music",
  "version": "1.0.0",
  "description": "",
  "main": "server.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1",
    "start": "node server.js",
    "json-server": "json-server --watch db.json --port 5000",
    "client": "npm start --prefix client",
    "dev": "concurrently \"npm run json-server\" \"npm run client\"",
    "heroku-postbuild": "npm install --prefix client && npm run build --prefix client"
  },
  "author": "",
  "license": "ISC",
  "dependencies": {
    "concurrently": "^6.0.2",
    "express": "^4.17.1",
    "json-server": "^0.16.3"
  }
}

try this尝试这个

app.use(express.static('client/build')));
    app.get('/*', function (req, res) {
        res.sendFile(path.join(__dirname, 'client','build', 'index.html'));
    });

and in ur package.json change to this在你的 package.json 更改为这个

"heroku_postbuild":"NPM_CONFIG_PRODUCTION=false npm install --prefix client && npm run build --prefix client" 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用 json-server 部署 reactJS 应用程序 - How to deploy reactJS app with json-server 我无法使用 json-server 部署我的 reactJS 应用程序 - I could not deploy my reactJS app with json-server 未找到 Heroku 错误模块“json-server” - Heroku error module not found 'json-server' 如何修复错误以将应用程序部署到 Heroku? - How to fix error to deploy app to Heroku? 当您将应用程序部署到 heroku 但应用程序无法启动时,如何修复“Express 不是构造函数”类型错误? - How to fix "Express is not a constructor" Type Error when you deploy your app to heroku but app can't start? 我尝试通过 npm install -g json-server 安装 json-server,但每次我都收到错误消息 - I trying to json-server by npm install -g json-server, but everytime I get an error 如何将 React 应用程序部署到 Heroku - How to deploy React application to Heroku 尝试在heroku中部署节点应用程序时不断收到应用程序错误 - Keep getting an application error when trying to deploy a node app in heroku 当我在heroku上部署我的nodejs应用程序时,构建成功但出现了“应用程序错误” - When I deploy my nodejs app on heroku, build success but got a “Application Error” 尝试将我的 React 应用程序部署到 Heroku 时出错 - Error when trying to deploy my React app to Heroku
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM