You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

PERN栈(React+Node+Express+PostgreSQL)cPanel部署数据库连接问题

PERN栈部署问题求助(React+Node/Express+PostgreSQL)

我是React Node/Express部署新手,使用PERN技术栈(React.js、Node.js、Express搭配PostgreSQL)开发的项目本地运行正常,但部署到托管服务器后出现以下问题:能通过终端连接数据库,能显示Node.js默认的"It works!"消息,但React仅能从3001端口获取数据,页面无法加载业务数据。

已执行的部署步骤

  • 生成React应用构建包(npm run build)并上传至/home/bidsvego/public_html/horserace/race/目录;
  • 通过cPanel创建Node应用(路径为public_html/horserace/nodeexpress/app.js),可正常获取默认输出"It works!";
  • 将自定义的app.js、horsemodel.js、package.json文件上传并覆盖至/home/bidsvego/public_html/horserace/nodeexpress目录;
  • 在cPanel中执行"Run NPM Install"操作;
  • 在Putty终端执行命令:source /home/bidsvego/nodevenv/public_html/horserace/nodeexpress/10/bin/activate && cd /home/bidsvego/public_html/horserace/nodeexpress;
  • 访问domainName/horserace/race可打开页面,但无法连接PostgreSQL;本地可连接托管数据库,但服务器端连接失败,页面仅显示HTML结构无数据;
  • 刷新页面时提示:Cannot GET /horserace/race/horse。

相关代码

app.js

const express = require("express");
const app = express();
const port = 3001;

const horseModel = require("./horseModel");
app.use(express.json());
app.use(function (req, res, next) {
  res.setHeader("Access-Control-Allow-Origin", "http://localhost:3000");
  res.setHeader("Access-Control-Allow-Methods", "GET,POST,PUT,DELETE,OPTIONS");
  res.setHeader(
    "Access-Control-Allow-Headers",
    "Content-Type, Access-Control-Allow-Headers"
  );
  next();
});

app.get("/", (req, res) => {
  horseModel
    .getHorses()
    .then((response) => {
      res.status(200).send(response);
    })
    .catch((error) => {
      res.status(500).send(error);
    });
});

app.get("/", (req, res) => {
  horseModel
    .getJockeys()
    .then((response) => {
      res.status(200).send(response);
    })
    .catch((error) => {
      res.status(500).send(error);
    });
});

app.listen(port, () => {
  console.log(`Server running on port ${port}, http://localhost:${port}`);
});

horsemodel.js

const Pool = require("pg").Pool;
const pool = new Pool({
  user: "kevin",
  host: "localhost",
  database: "localhost",
  password: "XXXXXXXX",
  port: 5432,
});

const getHorses = () => {
  return new Promise(function (resolve, reject) {
    pool.query(
      `SELECT * 
      FROM horses 
   
};

module.exports = {
  getHorses,
.
.
etc.
  // createHorse,
  // deleteHorse,
};

内容的提问来源于stack exchange,提问作者Kevin H

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.20 18:54:24