【问题标题】:after using this (http-proxy-middleware) it give me this error in reactjs使用这个(http-proxy-middleware)后,它在reactjs中给了我这个错误
【发布时间】:2020-03-19 10:57:36
【问题描述】:

我试图设置我的代理以与节点做出反应它试图在客户端的 package.json 中使用代理但是当我尝试使用 get 方法时它一直给我错误所以我搜索另一种解决方案并且我找到了这个使用这个 npm 模块(http-proxy-middleware)并运行 npm cache clean --force 命令来清除所有缓存,这样任何人都可以告诉我这个错误是什么,或者如果有任何其他方法我可以尝试制作我的应用程序工作正常

这是我的 server.js 文件

const express = require('express');
const connectDB = require('./config/db');
const app = express();

//connect database
connectDB();

// Init Middelware
app.use(express.json({ extended: false }));

const port = process.env.PORT || 6000;

app.get('/', (req, res) => {
  res.send('api running');
});

// Define route
app.use('/api/users', require('./router/api/user'));

app.use('/api/posts', require('./router/api/post'));

app.use('/api/profile', require('./router/api/profile'));

app.use('/api/auth', require('./router/api/auth'));

app.listen(port, () => {
  console.log(`server started in port ${port}`);
});

这是我的 setupProxy.js 文件

const proxy = require('http-proxy-middleware');
module.exports = function(app) {
  app.use(
    '/api',
    proxy({
      target: 'http://localhost:6000',
      changeOrigin: true
    })
  );
};

但它给了我另一个问题

[1] proxy is not a function
[1] npm ERR! code ELIFECYCLE
[1] npm ERR! errno 1
[1] npm ERR! social-app@0.1.0 start: `react-scripts start`
[1] npm ERR! Exit status 1
[1] npm ERR!
[1] npm ERR! Failed at the social-app@0.1.0 start script.
[1] npm ERR! This is probably not a problem with npm. There is likely additional logging output above.
[1]
[1] npm ERR! A complete log of this run can be found in:
[1] npm ERR!     C:\Users\hp\AppData\Roaming\npm-cache\_logs\2020-03-19T10_53_44_499Z-debug.log
[1] npm ERR! code ELIFECYCLE
[1] npm ERR! errno 1
[1] npm ERR! social-app@1.0.0 client: `npm start --prefix social-app`
[1] npm ERR! Exit status 1
[1] npm ERR!
[1] npm ERR! Failed at the social-app@1.0.0 client script.
[1] npm ERR! This is probably not a problem with npm. There is likely additional logging output above.
[1]
[1] npm ERR! A complete log of this run can be found in:
[1] npm ERR!     C:\Users\hp\AppData\Roaming\npm-cache\_logs\2020-03-19T10_53_44_587Z-debug.log
[1] npm run client exited with code 1

【问题讨论】:

    标签: javascript node.js reactjs proxy http-proxy-middleware


    【解决方案1】:

    使用新版本的http-proxy-middleware,您需要使用createProxyMiddleware

    const { createProxyMiddleware } = require('http-proxy-middleware');
    
    module.exports = function(app) {
      app.use(
        '/api',
        createProxyMiddleware({
           target: 'http://localhost:6000',
           changeOrigin: true
        })
      );
    };
    

    【讨论】:

    • 谢谢是问题所在,好吧,所以我试图从我的数据库中获取用户配置文件,但它给了我 400 错误(错误请求)所以可能是什么问题,我在邮递员中尝试过这条路线它工作正常
    • 我认为你没有正确使用 fetch 因为邮递员工作
    • 我发现我的数据库中没有配置文件可以获取,所以在我创建一个后它可以工作,谢谢 iamhuynq
    • 也为我工作过同样的问题
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-04-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多