[英]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.