我有一个安装了Nginx的网站作为ExpressJS服务器的保留代理(代理端口3001).这使用Node和ReactJS作为我的前端应用程序.
这只是一个测试网站,目前并不为任何用户所知或使用.我把它安装在带有Ubuntu的Digital Ocean Droplet上.
每天早上醒来的时候,我都会加载我的网站并查看502 Bad Gateway.问题是,我不知道如何找出这是怎么回事.我安装了PM2,它应该会自动重启我的ExpressJS服务器但是还没有这样做,当我运行时pm2 list,我的应用程序仍然显示online:
当我运行时pm2 logs,我收到以下错误(我以管理员身份运行):
所以我将pm2 restart all重新启动应用程序,但后来我没有看到任何崩溃信息.但是在这个场合拍摄这个截图时,有几个不寻常的请求./robots.txt,/sitemap.xml而且/.well-known/security.txt,没有任何迹象表明崩溃:
当我查看我的Nginx error.log文件时,我只能看到以下内容:
然而,在我的access.log([09/Oct/2018:06:33:19 +0000])中有一些模糊不清但我不知道这意味着什么:
如果我curl localhost:3001在服务器脱机时运行,我将收到连接错误消息.我运行后这很好用pm2 restart all.
我完全坚持这一点,即使是最小的帮助也会受到极大的赞赏,即使它只是告诉我,我正在完全吠叫错误的树,需要寻找其他地方 - 谢谢.
我创建了一个 React 应用程序,它使用fetch()路由文件componentDidMount来构建渲染组件中的数据。
这很好用,但是当 MySQL 数据库中的数据更新时,React 组件是静态的。
当 MySQL 数据库中的数据发生更改时,如何使用任何更改更新我的 React 组件?请在下面查看我的代码示例。
反应文件 - react.js
class myComponent extends React.Component {
constructor(props) {
super(props);
this.state = {
data: []
}
}
// componentDidMount - When the component is mounted.
componentDidMount() {
// Retrieve project data from the database.
fetch('/retrieve-data', {
credentials: 'include'
})
.then((response) => {
if (response.ok) {
return response.json();
} else {
console.log('Error with session response');
}
})
.then((result) => {
// Set the state …Run Code Online (Sandbox Code Playgroud) 在本地,我在端口 3001 上使用 ExpressJS,然后启动我的反应应用程序,npm start它在端口 3000 上运行开发服务器。这允许我将请求作为代理从 3000 路由到 3001。
对于生产,我在 DigitalOcean Droplet 上的 16.04 上安装了 Ubuntu NodeJS 6.12.13,然后安装了 Nginx 和 PM2。
在我的 Nginxdefault文件中,我设置了以下内容:
location / {
proxy_pass http://localhost:3001;
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;
}
Run Code Online (Sandbox Code Playgroud)
我已经移动了我的 Express 和 React 设置并将 Express 服务器添加到 PM2 启动。Nginx 被用作反向代理服务器以在端口 3001 上使用 Express。这里是 PM2 启动(www是运行 Express 的服务器文件的名称)。
当我加载我的域时,我会收到 Express 默认页面:
现在我不确定如何启动 react 应用程序,因为使用它启动它npm start并保持终端为生产服务器打开似乎不合逻辑。当我访问域而不是 Express 消息时,我需要查看我的 React 应用程序。
我找到了提到要使用的文章, …
我已经创建起反应,因此当存储在用户登录时重新加载页面,我已经加入,如果存在会话,然后要么应该检查功能的会话的登录系统setState(),以true或false。
由于我是React的新手,所以我不确定如何执行此功能。请在下面查看我的App.js代码:
import React from 'react';
import './css/App.css';
import LoginForm from "./LoginForm";
import Dashboard from "./Dashboard";
class App extends React.Component {
constructor(props) {
super(props)
this.state = {
renderLoginForm: true
};
this.handleLoginFormMount = this.handleLoginFormMount.bind(this);
}
handleLoginFormMount() {
this.setState({
renderLoginForm: false
});
}
// Check session function.
checkSession() {
fetch('/check-session', {
credentials: 'include'
})
.then((response) => {
return response.json();
})
.then((sessionResult) => {
if (sessionResult.username) {
console.log('false');
this.setState({
renderLoginForm: false
});
} else { …Run Code Online (Sandbox Code Playgroud) 在我的 React 应用程序中,我有一个侧边栏,当单击侧边栏关闭按钮时,需要添加一个 CSS 类。我正在用来React.createRef()创建对该元素的引用,但是,我收到以下错误:
这是我的代码:
import React from 'react';
import './css/Dashboard.css';
class Dashboard extends React.Component {
constructor(props) {
super(props);
this.sidebar = React.createRef();
}
sidebarClose() {
console.log('test');
this.sidebar.className += "hidden";
}
render() {
return (
<div id="dashboard">
<div ref={this.sidebar} id="sidebar">
<img width="191px" height="41px" src="logo.png"/>
<div onClick={this.sidebarClose} className="sidebar-close">X</div>
</div>
</div>
);
}
}
export default Dashboard;
Run Code Online (Sandbox Code Playgroud)
这console.log('test')只是为了让我可以确认该函数正在执行(确实如此)。
谢谢。