【发布时间】:2018-06-08 19:46:36
【问题描述】:
我编写了一个 React 应用程序,我想让它可以作为部署在 Web Nginx 服务器上的应用程序访问,该应用程序在 Reat App 上有一个链接。为此,我在 Nginx 中配置了一个反向代理。我的问题是,当我通过链接调用 Node/React 应用程序时,我只看到一个白页,如果我直接调用 Node/React 应用程序,所有这些都可以在浏览器中完美运行。 见下文:
package.json(服务器)
{
"name": "server",
"version": "1.0.0",
"description": "",
"main": "index.js",
"engines": {
"node": "9.8.0",
"npm": "6.0.0"
},
"scripts": {
"start": "node index.js",
"server": "nodemon index.js",
"client": "npm run start --prefix client",
"dev": "concurrently \"npm run start\" \"npm run client\""
},
"author": "",
"license": "ISC",
"dependencies": {
"express": "^4.16.3",
"mongoose": "^5.1.4",
"nodemon": "^1.17.5"
}
}
index.js(服务器)
const express = require("express");
const app = express();
const keys = require('./config/Keys');
const mongoose = require("mongoose");
require('./models/User');
require('./models/Profile');
mongoose.connect(keys.mongoURI);
const User = mongoose.model('users');
app.use(express.static('client/build'));
const path = require('path');
app.get('*', (req, res) => {
res.sendFile(path.resolve(__dirname,'client', 'build', 'index.html'));
});
package.json(客户端)
{
"name": "client",
"version": "0.1.0",
"private": true,
"homepage": ".",
"proxy": {
"/api/*": {
"target": "http://localhost:5000"
}
},
"dependencies": {
"react": "^16.4.0",
"react-dom": "^16.4.0",
"react-redux": "^5.0.7",
"react-router-dom": "^4.3.1",
"react-scripts": "1.1.4",
"redux": "^4.0.0"
},
"scripts": {
"start": "set HTTPS=true&&react-scripts start",
"build": "react-scripts build",
"test": "react-scripts test --env=jsdom",
"eject": "react-scripts eject"
}
}
index.js(客户端)
import materializeCSS from 'materialize-css/dist/css/materialize.min.css';
import React from 'react';
import ReactDOM from 'react-dom';
import { Provider } from 'react-redux';
import { createStore, applyMiddleware } from 'redux';
import reduxThunk from 'redux-thunk';
import App from './components/App';
const store = createStore(() => [], {}, applyMiddleware(reduxThunk));
ReactDOM.render(
<Provider store={store}><App/></Provider>,
document.querySelector('#root'));
Nginix 反向代理
`位置/反应{
proxy_pass http://10.30.1.185:5000;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection 'upgrade';
proxy_set_header Host $host;
proxy_cache_bypass $http_upgrade;
}`
在浏览器控制台中,我收到以下警告:
Loading failed for the <script> with source “https://snapmed.ca/static/js/main.cfe29440.js”.
任何想法为什么我只得到一个白页?
【问题讨论】: