Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/meteor/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 在使用这个(http代理中间件)之后,它在reactjs中给了我这个错误_Javascript_Node.js_Reactjs_Proxy_Http Proxy Middleware - Fatal编程技术网

Javascript 在使用这个(http代理中间件)之后,它在reactjs中给了我这个错误

Javascript 在使用这个(http代理中间件)之后,它在reactjs中给了我这个错误,javascript,node.js,reactjs,proxy,http-proxy-middleware,Javascript,Node.js,Reactjs,Proxy,Http Proxy Middleware,我试图设置我的代理,使其与节点反应。它试图在客户端的package.json中使用代理,但当我尝试使用get方法时,它不断给我错误,因此我搜索另一个解决方案,并使用此npm模块(http代理中间件)找到了此解决方案我运行npm cache clean--force命令来清除所有缓存,这样任何人都可以告诉我这个错误是什么,或者是否有其他方法可以让我的应用程序正常工作 这是我的server.js文件 const express = require('express'); const connectD

我试图设置我的代理,使其与节点反应。它试图在客户端的package.json中使用代理,但当我尝试使用get方法时,它不断给我错误,因此我搜索另一个解决方案,并使用此npm模块(http代理中间件)找到了此解决方案我运行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

有了新版本的
http代理中间件
,您需要使用
createProxyMiddleware

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

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

谢谢,这就是问题所在,好吧,所以我试着从我的数据库中获取用户配置文件,但它给了我400个错误(错误请求),那么问题出在哪里,我在《邮递员》中尝试过这条路线,效果很好。我认为您没有正确使用fetch,因为邮递员工作。我发现我的数据库中没有可获取的配置文件,所以在我创建了一个it工作之后,感谢iamhuynqWorked为我解决了同样的问题