微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

每次尝试在我的网站上发布内容时都会收到 504 错误

如何解决每次尝试在我的网站上发布内容时都会收到 504 错误

我遇到这个错误已经有一段时间了,所以我想我有一些设置错误而不是系统中的错误。一切都在本地主机上运行,​​我只是很难在谷歌云上启动并运行它。你可以在这里看到它http://35.194.72.130/。在左边,我试图将信息发布到数据库。在我的 util.js 中,我有

import "isomorphic-fetch"

export function addTime(name,time) {
    return fetch('http://35.194.72.130:3001/api/addtime',{
        method: 'POST',headers: { 'Content-Type': 'application/json' },body: JSON.stringify({ name,time }) 
    })
}

然后按照前任教授的指示,我添加文件 /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 _;
            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;
    }
        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;
    }
}

我已尝试将 proxy_pass 更改为 http://35.194.72.130:3000,但该站点无法加载。它通过 times 正确接收 "http://35.194.72.130:3001/api/list" 页面上的信息。您是否看到可能导致此 504 问题的原因?

编辑:server.js

const express = require("express");
const cors = require("cors");

const bodyParser = require("body-parser");
var dateFormat = require('dateformat');

const app = express();
app.set("port",3001);

app.use(bodyParser.json({ type: "application/json" }));
app.use(bodyParser.urlencoded({ extended: true }));

app.use(function(req,res,next) {
  res.header("Access-Control-Allow-Origin","*");
  res.header("Access-Control-Allow-Headers","Origin,X-Requested-With,Content-Type,Accept");
  next();
});

const Pool = require("pg").Pool;
const config = {
    host: "localhost",user: "Austin",password: "astros5",database: "cubing"
};

const pool = new Pool(config);

app.post("/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});

});

app.get("/list",res) => {
    const template = await pool.query('SELECT * FROM times ORDER BY time ASC');
    res.json({times: template.rows});

})

app.listen(app.get("port"),() => {
    console.log('Server at: http://localhost:${app.get("port")}/');
});

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。