简体   繁体   English

MERN && Heroku:我的应用程序显示在 VirtualBox/Linux 环境中,但不在 Windows 或移动设备上

[英]MERN && Heroku: My app displays on VirtualBox/Linux environment, but not on Windows or mobile

Update On the console in the windows browser(where the app is not loading, just the background image) I have an error:更新在 Windows 浏览器的控制台上(应用程序未加载,只是背景图像)我有一个错误:

Uncaught TypeError: Cannot read property 'apply' of undefined

This error is not showing up on the browser inside Ubuntu that it's actually working in. I'm investigating this这个错误没有显示在它实际工作的 Ubuntu 内的浏览器上。我正在调查这个


I'm using Oracle VM VirtualBox with Debian Ubuntu OS to develop this MERN app.我正在使用 Oracle VM VirtualBox 和 Debian Ubuntu OS 来开发这个 MERN 应用程序。 I pushed it up to Heroku.我把它推到了 Heroku。 The logs don't have any error msgs, and the website opens just fine inside the virtualbox.日志没有任何错误消息,网站在 virtualbox 内打开得很好。

But on my windows browser, it only displays the background image (slightly distorted), and does the same when I try to access it on mobile.但是在我的 Windows 浏览器上,它只显示背景图像(略微扭曲),并且当我尝试在移动设备上访问它时也是如此。

I had a lot of errors leading up to this point, originally my app was technically "working" but with Cannot get / error on the screen, that I figured out was actually working but just displaying the BackEnd half of my app (I tested by going to the URLs I have setup in Express, and my data was there)到目前为止,我有很多错误,最初我的应用程序在技术上是“工作的”,但是屏幕上出现了Cannot get /错误,我发现它实际上是在工作,但只是显示了我的应用程序的后端一半(我测试了转到我在 Express 中设置的 URL,我的数据就在那里)

So I installed cors and path (following another stackoverflow post) and configured server.js and now it sort of works.所以我安装了corspath (在另一个 stackoverflow 帖子之后)并配置了server.js ,现在它有点工作了。 But I'm not sure what the problem is.但我不确定问题是什么。

The app's structure is, in the root directory I have whatever backEnd stuff I need (server.js file, routes, models, modules etc) including a directory /client which is where the React front-end app is.该应用程序的结构是,在根目录中,我有我需要的任何后端内容(server.js 文件、路由、模型、模块等),包括一个目录 /client,这是 React 前端应用程序所在的目录。

My server.js file我的 server.js 文件

const mongoose = require('mongoose');
const express = require('express');
const app = express();
const config = require('config');
const cors = require('cors');
const path = require('path');

// Middleware
app.use(express.json());

// DB Config
// You can get all your config/ values with config.get('')
const db = config.get('mongoURI');

// Connect to MongoDB
mongoose
    .connect(process.env.URI || db, { 
        useNewUrlParser: true,
        useUnifiedTopology: true,
        useCreateIndex: true
    })
    .then(() => console.log("Connected to MongoDB.."))
    .catch(err => console.log(err));

// Available Routes
const tournaments = require('./routes/api/tournaments');
const users = require('./routes/api/users');
// Use Routes
app.use(cors());
app.use('/tournaments', tournaments);
app.use('/users', users);

// For Heroku deployment
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'))
    });
};

// Run Server
const port = process.env.PORT || 5000;
app.listen(port, () => console.log(`Server started on port: ${port}`));

My package.json我的 package.json

{
  "name": "smash-hosting",
  "version": "1.0.0",
  "description": "",
  "engines": {
    "node": "12.16.2"
  },
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1",
    "client-install": "cd client && npm install",
    "start": "node server.js",
    "server": "nodemon server.js",
    "client": "cd client && npm start",
    "dev": "concurrently \"npm run server\" \"npm run client\"",
    "heroku-postbuild": "cd client && npm install && npm run build"
  },
  "keywords": [],
  "author": "",
  "license": "ISC",
  "dependencies": {
    "bcryptjs": "^2.4.3",
    "concurrently": "^5.0.0",
    "config": "^3.2.4",
    "cors": "^2.8.5",
    "express": "^4.17.1",
    "jsonwebtoken": "^8.5.1",
    "moment": "^2.26.0",
    "mongoose": "^5.7.12",
    "nodemon": "^1.19.4",
    "path": "^0.12.7",
    "react-icons": "^3.10.0",
    "react-tournament-bracket": "^0.2.4"
  },
  "devDependencies": {
    "nodemon": "^1.19.4"
  }
}

It seems the server is running but can't get the client to connect.服务器似乎正在运行,但无法让客户端连接。 Bash command && (and) didn't work in windows. Bash 命令&& (和)在 Windows 中不起作用。 Replace with a single & operator.替换为单个&运算符。

Solved the problem!解决了问题!

The issue was with Redux apparently.问题显然出在 Redux 上。 I noticed in the browser where my app was not rendering properly, there was an error in the console: Uncaught TypeError: Cannot read property 'apply' of undefined我注意到在浏览器中我的应用程序没有正确呈现,控制台中有一个错误: Uncaught TypeError: Cannot read property 'apply' of undefined

That led me to https://github.com/reduxjs/redux/issues/2359 which has a number of various solutions.这让我找到了https://github.com/reduxjs/redux/issues/2359 ,它有许多不同的解决方案。

Ultimately, removing the REDUX_EXTENSION_TOOLS code from the redux store allowed my heroku app to render properly on different browsers including my phone.最终,从 redux 商店中删除 REDUX_EXTENSION_TOOLS 代码允许我的 heroku 应用程序在包括我的手机在内的不同浏览器上正确呈现。

store.js file: store.js 文件:

import { createStore, applyMiddleware, compose } from 'redux';
import thunk from 'redux-thunk';
import rootReducer from './reducers';
const initialState = {};
const middleware = [thunk];
const store = createStore(
    rootReducer,
    initialState,
    compose(
        applyMiddleware(...middleware)
        // window.__REDUX_DEVTOOLS_EXTENSION__ && window.__REDUX_DEVTOOLS_EXTENSION__()
    )
);
export default store;

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM