获得“无法获得 /iSfavorite”在我的node.js应用程序上托管在digitalocean上

发布于 2025-02-01 05:39:09 字数 2398 浏览 4 评论 0原文

为什么我会遇到以下错误?我有一个在Digitalocean上托管的小型Nodejs应用程序。当我在Postman中运行以下帖子命令时,我会遇到以下错误:“ http://octopus-app-s7q5v.ondigitalocean.app:8080/isfavorite”带有hody json参数

{
    "userid": "101",
    "petid": "1"
}

:该应用程序是app.post(“/isfavorite”)而不是app.get。它连接到了MySQL数据库,该数据库也托管在Digitalocean上。我在下面的应用程序中包含了一个小版本的代码。为什么我会遇到此错误?当我添加一个app.get(“/”)并返回可行的“ hello”响应时。它在本地运行。我已经在Digitalocean设置中配置了所有环境变量。我的nodejs应用程序的根部有“ ca-certificate.crt”。 Digitalocean App Platform表示,它可以在服务器上构建和部署正常。

另外,关于我添加到代码的console.logs,它们存储在哪里?我可以看到启动Web服务器并倾听但没有其他任何一个的人。该应用在端口8080上侦听,IP地址为0.0.0.0。

const bodyParser = require("body-parser");
const querystring = require('querystring');
const router = express.Router();
const app = express();
app.use("/", router);

const mysql = require('mysql');
const fs = require('fs');

app.use(bodyParser.urlencoded({ extended: false }));
app.use(bodyParser.json());
app.use(express.json());

app.post("/isFavorite", (req, res) => {
    try {
        console.debug("isFavorite Start");

        const conn = mysql.createConnection({
            host: process.env.DBHOST,
            port: process.env.DBPORT,
            user: process.env.DBUSER,
            database: process.env.DBNAME,
            password: process.env.DBPASSWORD
        });

        console.log("isFavorite logged on to db");

        let selectQuery = 'SELECT COUNT(*) c FROM Favorites WHERE userID = ? AND petID = ?';
        let query = mysql.format(selectQuery,[req.body.userid, req.body.petid]);
        conn.query(query,(err, response, fields) => {
            if(err) {
                console.log(err);
                res.status(500).send("Error querying database.");
                return;
            }
            // rows added
            console.log("isFavorite");
            console.log(response.length);
            if (response.length > 0) {
                console.log(JSON.stringify(response));
                res.status(200).json({IsFavorite: response[0].c >= 1});
            } else {
                res.status(200).json({IsFavorite: false});
            }
    });
    } catch (err) {
        next(err);
    }
});

let PORT = process.env.PORT || 3000;
let IP = process.env.IP || '127.0.0.1';
app.listen(PORT, IP, () => {
    console.log('Server is running at port ' + PORT + ' and IP = ' + IP);
});

Why am I getting the following error? I have a small Nodejs app hosted on DigitalOcean. I am getting the following error "Cannot GET /isFavorite" when I run the following post command in Postman: "http://octopus-app-s7q5v.ondigitalocean.app:8080/isFavorite" with body JSON parameters of:

{
    "userid": "101",
    "petid": "1"
}

The route I have for the app is an app.post("/isFavorite") and not an app.get. It is connected to a MySQL database which is also hosted on DigitalOcean. I have included a small version of the code for the app below. Why would I be getting this error? When I add an app.get("/") and return a response of "Hello" that works. It runs locally. I have configured all the environment variables in DigitalOcean settings. I have the "ca-certificate.crt" in the root of my Nodejs app. DigitalOcean app platform says it builds and deploys fine on the server.

Also, regarding the console.logs I have added to the code, where are they stored? I can see the one which starts the webserver and listens but not any of the others. The app is listening on port 8080 and has an IP address of 0.0.0.0.

const bodyParser = require("body-parser");
const querystring = require('querystring');
const router = express.Router();
const app = express();
app.use("/", router);

const mysql = require('mysql');
const fs = require('fs');

app.use(bodyParser.urlencoded({ extended: false }));
app.use(bodyParser.json());
app.use(express.json());

app.post("/isFavorite", (req, res) => {
    try {
        console.debug("isFavorite Start");

        const conn = mysql.createConnection({
            host: process.env.DBHOST,
            port: process.env.DBPORT,
            user: process.env.DBUSER,
            database: process.env.DBNAME,
            password: process.env.DBPASSWORD
        });

        console.log("isFavorite logged on to db");

        let selectQuery = 'SELECT COUNT(*) c FROM Favorites WHERE userID = ? AND petID = ?';
        let query = mysql.format(selectQuery,[req.body.userid, req.body.petid]);
        conn.query(query,(err, response, fields) => {
            if(err) {
                console.log(err);
                res.status(500).send("Error querying database.");
                return;
            }
            // rows added
            console.log("isFavorite");
            console.log(response.length);
            if (response.length > 0) {
                console.log(JSON.stringify(response));
                res.status(200).json({IsFavorite: response[0].c >= 1});
            } else {
                res.status(200).json({IsFavorite: false});
            }
    });
    } catch (err) {
        next(err);
    }
});

let PORT = process.env.PORT || 3000;
let IP = process.env.IP || '127.0.0.1';
app.listen(PORT, IP, () => {
    console.log('Server is running at port ' + PORT + ' and IP = ' + IP);
});

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文