问题描述
我一直在尝试使用 json 服务器和 react,但由于某种原因,我在 Heroku 上不断收到应用程序错误。可以使用 json-server 进行测试吗。我已经分享了我的 server.js、文件结构和 package.json
server.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);
我的文件夹结构
----client
----node modules
----public
----src
----packgage.json
----db.json
----packgae.json
----server.js
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"
}
}
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)