简体   繁体   English

将请求 localhost:3000/api/product 代理到 http://localhost:5000/ 时发生错误

[英]Error occurred while proxying request localhost:3000/api/product to http://localhost:5000/

We are trying to create an simple eCommerce app using Typescript, express and nextjs.我们正在尝试使用 Typescript、express 和 nextjs 创建一个简单的电子商务应用程序。

When we make a request to it, it throws the following error.当我们向它发出请求时,它会抛出以下错误。

[HPM] Error occurred while proxying request localhost:3000/api/product to http://localhost:5000/ [ECONNREFUSED] ( https://nodejs.org/api/errors.html#errors_common_system_errors ) [HPM] 将请求 localhost:3000/api/product 代理到 http://localhost:5000/ [ECONNREFUSED] ( https://nodejs.org/api/errors.html#errors_common_system_errors ) 时出错

My friend is using Windows, and code is working on his PC but not on my Ubuntu desktop.我的朋友使用的是 Windows,代码在他的 PC 上运行,但在我的 Ubuntu 桌面上运行。

I tried killing the port like below.我尝试像下面这样杀死端口。 It also kills the port but this does not help.它也会杀死端口,但这无济于事。

$ sudo lsof -t -i:5000
6033
$sudo kill -9 6033

$sudo lsof -t -i:3000
6101
$sudo kill -9 6101

Proxy server is listening at server.js file as:代理服务器正在监听 server.js 文件:

const express = require('express');
const next = require('next');
const { createProxyMiddleware } = require('http-proxy-middleware');

const dev = process.env.NODE_ENV !== 'production';
const app = next({ dev });
const handle = app.getRequestHandler();

app
  .prepare()
  .then(() => {
    const server = express();
    // apply proxy in dev mode
    if (dev) {
      server.use(
        '/api',
        createProxyMiddleware({
          target: 'http://localhost:5000',
          changeOrigin: true,
        })
      );
    }

    server.all('*', (req, res) => {
      return handle(req, res);
    });

    server.listen(3000, (err) => {
      if (err) throw err;
      console.log('> Ready on http://localhost:5000');
    });
  })
  .catch((err) => {
    console.log('Error', err);
  });

Found the solution.找到了解决办法。

I have ignore node_module and upload folder in .gitignore我忽略了node_module和 .gitignore 中的上传文件夹

I forgot to add the folder after cloning.克隆后我忘了添加文件夹。 So, it was showing proxy error.所以,它显示代理错误。

After I add upload folder error was resolved.添加上传文件夹后,错误已解决。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Docker 代理错误:无法从 localhost:3000 代理请求 /api/product/B002QYW8LW 到 http://localhost:5000 - Docker proxy error: Could not proxy request /api/product/B002QYW8LW from localhost:3000 to http://localhost:5000 出现错误:无法将请求/ api / plaid / accounts从localhost:3000代理到http:// localhost:5000 / - getting error : Could not proxy request /api/plaid/accounts from localhost:3000 to http://localhost:5000/ “React 代理错误:无法将请求 /api/ 从 localhost:3000 代理到 http://localhost:5000 (ECONNREFUSED)”?错误.. 没有在线解决方案 - "React Proxy error: Could not proxy request /api/ from localhost:3000 to http://localhost:5000 (ECONNREFUSED)'? Error.. No solution online 代理错误:无法代理请求 /api/house-listing 从 localhost:3000 到 http://localhost:5000? (ECONNRESET)、MERN - Proxy error: Could not proxy request /api/house-listing from localhost:3000 to http://localhost:5000? (ECONNRESET), MERN http://localhost:3000/localhost:5000/api/register 当我请求 URL - http://localhost:3000/localhost:5000/api/register when i request URL 代理错误:无法代理请求/用户从 localhost:3000 到 http://localhost:5000/ - Proxy error: Could not proxy request /users from localhost:3000 to http://localhost:5000/ 连接到http:// localhost:3000 / api / contact时出错 - there was error connecting to http://localhost:3000/api/contact React 代理错误:无法代理请求 /api/ 从 localhost:3000 到 http://localhost:8000 (ECONNREFUSED) - React Proxy error: Could not proxy request /api/ from localhost:3000 to http://localhost:8000 (ECONNREFUSED) 尝试将请求 /api/v1/mega/building 从 localhost:4200 代理到 http://localhost:8080 时发生错误(ECONNREFUSED) - Error occurred while trying to proxy request /api/v1/mega/building from localhost:4200 to http://localhost:8080 (ECONNREFUSED) CORS 政策已阻止从源“http://localhost:3000”访问“http://localhost:5000/api/products”处的 XMLHttpRequest:否“访问控制” - Access to XMLHttpRequest at 'http://localhost:5000/api/products' from origin 'http://localhost:3000' has been blocked by CORS policy: No 'Access-Contr
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM