尝试代理请求 React/ Express (npm run start) 时发生错误 (ECONNREFUSED)

发布于 2025-01-15 10:21:37 字数 1933 浏览 1 评论 0原文

你好,希望你一切都好。

在我的项目上运行 npm run start 时,它不起作用,因为它似乎只构建文件一次并且不监视它。所以它不编译FE部分,只运行服务器端。

package.json

"scripts": {
    "build": "node build.js",
    "start": "node index.js",
    "test": "jest",
    "lint": "eslint . --ext=js,jsx,mjs --max-warnings=0 --color || true",
    "lint:fix": "eslint . --ext=js,jsx,mjs --max-warnings=0 --color --fix || true"
},

bundle-server.js

const app = express();

app.use(require("./build.js"));

app.listen(8081, () => console.log("Ready to compile and serve bundle.js"));

index.js

    app.use(
        "/bundle.js",
        require("http-proxy-middleware")({
            target: "http://localhost:8081/"
        })
    );
} else {
    app.use("/bundle.js", (req, res) => res.sendFile(`${__dirname}/bundle.js`));
}

///////////-------///////////

if (require.main == module) {
    app.listen(process.env.PORT || 8080, () => console.log("Server Listening"));
}

还有一个 db.js

const db = spicedPg(
    process.env.DATABASE_URL ||
    "postgres:postgres:postgres@localhost:5432/contacts"

);

运行时 < code>npm start 或 npm run start,显示以下错误:

Server Listening
[HPM] Error occurred while trying to proxy request /bundle.js from localhost:8080 to http://localhost:8081/ (ECONNREFUSED) (https://nodejs.org/api/errors.html#errors_common_system_errors)

我不确定这是否可能来自,

const db = spicedPg(
   process.env.DATABASE_URL ||
   "postgres:postgres:postgres@localhost:5432/contacts"

因为我没有传递正确的引用来调用数据库?此外,这是一个生产设置,而不是开发设置,因此这也可能是源代码。

我应该在代码库中哪里查找来解决这个 ECONNREFUSED 问题?

请在此处查找存储库,

感谢您抽出时间

Hello and hope you are well.

When running npm run start on my project, it is not working because it seems to build the files only once and not watch for it. So it is not compiling the FE part and only running server side.

package.json

"scripts": {
    "build": "node build.js",
    "start": "node index.js",
    "test": "jest",
    "lint": "eslint . --ext=js,jsx,mjs --max-warnings=0 --color || true",
    "lint:fix": "eslint . --ext=js,jsx,mjs --max-warnings=0 --color --fix || true"
},

bundle-server.js

const app = express();

app.use(require("./build.js"));

app.listen(8081, () => console.log("Ready to compile and serve bundle.js"));

index.js

    app.use(
        "/bundle.js",
        require("http-proxy-middleware")({
            target: "http://localhost:8081/"
        })
    );
} else {
    app.use("/bundle.js", (req, res) => res.sendFile(`${__dirname}/bundle.js`));
}

///////////-------///////////

if (require.main == module) {
    app.listen(process.env.PORT || 8080, () => console.log("Server Listening"));
}

there is as well a db.js

const db = spicedPg(
    process.env.DATABASE_URL ||
    "postgres:postgres:postgres@localhost:5432/contacts"

);

When running npm start or npm run start, the following error is shown:

Server Listening
[HPM] Error occurred while trying to proxy request /bundle.js from localhost:8080 to http://localhost:8081/ (ECONNREFUSED) (https://nodejs.org/api/errors.html#errors_common_system_errors)

I am not sure if this could be from the

const db = spicedPg(
   process.env.DATABASE_URL ||
   "postgres:postgres:postgres@localhost:5432/contacts"

as I am not passing the correct ref to call the database ? In addition, this is a production set up and not a dev one, so this could be the source too.

Where should I look in the code base to tackle this ECONNREFUSED ?

Please find the repo here

Thank you for taking the time

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

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

发布评论

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