Поэтому я пытался развернуть простое приложение MERN на heroku. Я смог сделать sh его и запустил свой веб-сайт, но после открытия при открытии он появляется на пустой странице:
{
"message": "Not Found"
}
Это отображается в журналах моей героки после выполнения git pu sh heroku master:
2020-02-29T13:59:10.000000+00:00 app[api]: Build succeeded
2020-02-29T13:59:18.850814+00:00 heroku[web.1]: Starting process with command `npm start`
2020-02-29T13:59:20.750984+00:00 app[web.1]:
2020-02-29T13:59:20.751002+00:00 app[web.1]: > mediamarket@1.0.0 start /app
2020-02-29T13:59:20.751003+00:00 app[web.1]: > node server.js
2020-02-29T13:59:20.751003+00:00 app[web.1]:
2020-02-29T13:59:21.438469+00:00 app[web.1]: MongoDB database connection established successfully
2020-02-29T13:59:21.714813+00:00 heroku[web.1]: State changed from starting to up
2020-02-29T13:59:22.774215+00:00 heroku[router]: at=info method=GET path="/" host=mediamarket.herokuapp.com request_id=580f0e5b-22ef-40aa-86e9-0f0092d07f6f fwd="49.145.135.115" dyno=web.1 connect=0ms service=8ms status=404 bytes=269 protocol=https
2020-02-29T13:59:28.654728+00:00 heroku[router]: at=info method=GET path="/" host=mediamarket.herokuapp.com request_id=cd7eaf81-e4a3-48a1-80eb-759dfc0ceefa fwd="49.145.135.115" dyno=web.1 connect=1ms service=1ms status=404 bytes=269 protocol=https
2020-02-29T13:59:42.113260+00:00 heroku[router]: at=info method=GET path="/" host=mediamarket.herokuapp.com request_id=614576fd-41dc-45e1-9d29-7f14bef7b71d fwd="49.145.135.115" dyno=web.1 connect=0ms service=2ms status=404 bytes=269 protocol=https
2020-02-29T13:59:46.963639+00:00 heroku[router]: at=info method=GET path="/" host=mediamarket.herokuapp.com request_id=b717dad5-7281-48da-9782-2afb115ca2e1 fwd="49.145.135.115" dyno=web.1 connect=0ms service=2ms status=404 bytes=269 protocol=https
2020-02-29T14:34:56.346013+00:00 heroku[web.1]: Idling
2020-02-29T14:34:56.349138+00:00 heroku[web.1]: State changed from up to down
2020-02-29T14:34:57.041824+00:00 heroku[web.1]: Stopping all processes with SIGTERM
2020-02-29T14:34:57.108434+00:00 heroku[web.1]: Process exited with status 143
Это мой пакет. json файл на стороне сервера:
Root папка пакета. json
{
"name": "mediamarket",
"version": "1.0.0",
"description": "",
"main": "server.js",
"scripts": {
"client-install": "npm install --prefix client",
"build": "concurrently \"cd client && npm run build\" \"npm build \"",
"test": "echo \"Error: no test specified\" && exit 1",
"client": "cd ./client && npm start ",
"server-prod": "node server.js",
"server": "nodemon server.js",
"start": "node server.js",
"start:dev": "concurrently --kill-others \"npm run client\" \"npm run server\" ",
"heroku-postbuild": "NPM_CONFIG_PRODUCTION=false npm install --prefix client && npm run build --prefix client"
},
"author": "",
"license": "ISC",
"dependencies": {
"bcrypt": "^3.0.6",
"concurrently": "^5.1.0",
"cors": "^2.8.5",
"express": "^4.17.1",
"jsonwebtoken": "^8.5.1",
"mongoose": "^5.7.1",
"nodemon": "^1.19.2",
"path": "^0.12.7"
}
}
Это мой пакет. json файл на стороне клиента:
клиент / пакет. json
{
"name": "client",
"version": "0.1.0",
"private": true,
"dependencies": {
"@moneybutton/react-money-button": "^0.30.0",
"react": "^16.9.0",
"react-dom": "^16.9.0",
"react-redux": "^7.1.1",
"react-router-dom": "^5.1.0",
"react-scripts": "3.1.2",
"redux": "^4.0.4",
"redux-thunk": "^2.3.0"
},
"scripts": {
"start": "react-scripts start",
"build": "react-scripts build",
"test": "react-scripts test",
"eject": "react-scripts eject"
},
"eslintConfig": {
"extends": "react-app"
},
"browserslist": {
"production": [
">0.2%",
"not dead",
"not op_mini all"
],
"development": [
"last 1 chrome version",
"last 1 firefox version",
"last 1 safari version"
]
},
"proxy": "http://localhost:2019"
}
My app.js and server.js are shown below:
приложение. js
const express = require('express');
const app = express();
const mongoose = require('mongoose');
const cors = require('cors');
const authenticate = require('./api/middleware/authenticate');
const path = require('path');
mongoose.connect('mongodb+srv://<Username>:<Password>@cluster0-fn5wo.mongodb.net/test?retryWrites=true&w=majority', { useUnifiedTopology: true, useNewUrlParser: true, useCreateIndex: true });
const connection = mongoose.connection;
connection.once('open', () => {
console.log("MongoDB database connection established successfully");
})
const adminRoutes = require('./api/routes/admins');
const categoryRoutes = require('./api/routes/categories');
const userRoutes = require('./api/routes/users');
const productRoutes = require('./api/routes/products');
const cartItemRoutes = require('./api/routes/cartItems');
const orderRoutes = require('./api/routes/orders');
app.use(cors());
app.use(express.json());
app.use('/admin', adminRoutes);
app.use('/category', categoryRoutes);
app.use('/user', userRoutes);
app.use('/products', productRoutes);
app.use('/cart', authenticate, cartItemRoutes);
app.use('/order', authenticate, orderRoutes);
app.use((req, res, next) => {
res.status(404).json({
message: 'Not Found'
})
})
app.use(express.static(path.join(__dirname, 'client/build')));
if(process.env.NODE_ENV === 'production') {
app.use(express.static(path.join(__dirname, 'client/build')));
//
app.get('*', (req, res) => {
res.sendfile(path.join(__dirname = 'client/build/index.html'));
})
}
//build mode
app.get('*', (req, res) => {
res.sendFile(path.join(__dirname+'/client/public/index.html'));
})
module.exports = app;
сервер. js
const http = require('http');
const app = require('./app');
const port = process.env.PORT || 2019;
const server = http.createServer(app);
server.listen(port);
Пожалуйста, помогите мне, если можете, и Спасибо.