如何解决使用 nginx 将我的数据库正确连接到我的网站
每当我尝试将内容发布到数据库时,我的网站 http://35.221.40.223/ 都会提供 e Failed to load resource: the server responded with a status of 502 (Bad Gateway)
。它适用于本地主机,所以我假设问题在于实际让它在线。如果你到这里 http://35.221.40.223/api/list 你可以看到数据库是空的。我的 /etc/nginx/sites-available/default
如下所示,几乎只是按照学校上一堂课的说明进行操作。
server {
listen 80 default_server;
listen [::]:80 default_server;
root /var/www/html;
# Add index.php to the list if you are using PHP
index index.html index.htm index.nginx-debian.html;
server_name 35.221.40.223;
location /api {
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;
proxy_connect_timeout 240;
proxy_send_timeout 240;
proxy_read_timeout 240;
send_timeout 240;
}
location / {
proxy_pass http://localhost:3000;
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;
}
}
App.js
import React from 'react';
import { addTime } from "./util";
function App() {
const [name,setName] = React.useState("")
const [time,setTime] = React.useState("")
function handleUpdate(evt) {
setName(evt.target.value);
}
function handleUpdateTime(evt) {
setTime(evt.target.value);
}
async function handleAddTime(evt) {
await addTime(name,time);
}
return <div>
<p><input type='text' value={time} onChange={handleUpdateTime} /></p>
<p><input type='text' value={name} onChange={handleUpdate} /></p>
<button className='button-style' onClick={handleAddTime}>Add Time</button>
</div>
}
export default App;
util.js
import "isomorphic-fetch"
export function addTime(name,time) {
return fetch('http://35.221.40.223/api/addtime?name=${name}&time=${time}',{
method: 'POST',headers: { 'Content-Type': 'application/json' },body: JSON.stringify({ name,time })
})
}
我的节点文件在哪里发布:
app.post("/api/addtime",cors(),async (req,res) => {
const name = req.body.name;
const time = req.body.time;
// const timeStamp = dateFormat(time,dateFormat.masks.isoDateTime);
const template = 'INSERT INTO times (name,time) VALUES ($1,$2)';
const response = await pool.query(template,[name,time]);
res.json({name: name,time: time});
});
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。