'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
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);
my folder structure
----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"
}
}
Solution 1:[1]
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
"heroku_postbuild":"NPM_CONFIG_PRODUCTION=false npm install --prefix client && npm run build --prefix client"
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 |