简体   繁体   English

Webpack-dev-server config用于将API调用从react-router路由到node / express路由器

[英]Webpack-dev-server config to route API calls from react-router to node/express router

I have a client react/redux application that needs to gather data from my node express with express router. 我有一个客户端react / redux应用程序需要通过快速路由器从我的节点快递收集数据。

On development environment we use webpack-dev-server, that does not route the /api calls to the express/node. 在开发环境中,我们使用webpack-dev-server,它不会将/ api调用路由到express / node。 So we need to configure a proxy on devserver. 所以我们需要在devserver上配置代理。

Here is the configuration: 这是配置:

  devServer: {
      proxy: { 
        '/api/*': 'http://localhost:8080'
      }    

Every /api/whatever/whatever or api/whatever call must be redirected to node/express. 每个/ api / any / whatever或者api /无论什么电话都必须重定向到node / express。

I´m getting the following error on webpack console: 我在webpack控制台上收到以下错误:

[HPM] Error occurred while trying to proxy request /api/admin/user from localhost:8080 to http://localhost:8080 (ENOBUFS) (https://nodejs.org/api/errors.html#errors_common_system_errors)

Questions: 问题:

  • What does ENOBUFS means in the error code ? ENOBUFS在错误代码中的含义是什么?
  • Shall I need to configure a different port or a different node/express service to serve the API ? 我需要配置不同的端口或不同的节点/快速服务来提供API吗?
  • How do I configure the proxy to call the express/router APIs ? 如何配置代理以调用快速/路由器API?

server.js server.js

app.get("/api/data", (req, res) => {
  res.send("hi from server"); // replace me with real data
});

webpack.config webpack.config

//need the ^ before your path name
proxy: {
      "^/api/*": {
        target: "http://localhost:3001/api/",
        secure: false
      }
    }

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM