"反应代理错误:无法将请求/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

查看:120
本文介绍了"反应代理错误:无法将请求/api/从localhost:3000代理到http://localhost:5000(ECONNREFUSED)?错误..没有在线解决方案的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将我的React前端连接到后端,我尝试了所有操作,但仍然无法正常工作.我添加了"proxy":"localhost:5000" 也没有运气我尝试过

I am trying to connect my react frontend to the backend i tried everything but its still not working. I added "proxy" : "localhost:5000" and no luck. I tried

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

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

,也没有运气.有没有人找到解决问题的办法.我到处都是stackoverflow

and also no luck. Has anyone found a solution to the problem. I looked all over stackoverflow

更新:索引文件

// Imports 
const express = require('express')
const app = express();
const cors = require('cors')
const mongoose = require('mongoose');
const bodyParser = require('body-parser')
const cookieParser = require('cookie-parser')
const config = require('./config/key')
const routes = require('./routes/api')
require('dotenv').config()
// -----Connect to the database
mongoose.connect(config.mongoURI,
    {useNewUrlParser: true,useUnifiedTopology:true,
    createIndexes : true}).then(()=>{
    console.log("Database connected")
}).catch((error)=>{
    console.log(error)
})

// ----setting up middleware 
const corsOptions = {
    origin:['http://localhost:3000'],
    credentials: true,
    optionsSuccessStatus: 200
}
app.use(cors(corsOptions))
app.use(bodyParser.urlencoded({extended : true}))
app.use(bodyParser.json())
app.use(cookieParser())
app.use('/api', routes);


// ---- Setting up server to listen on port 5000
app.listen(5000,()=>{
    console.log("App is running on 5000")
})

推荐答案

对我来说,问题是端口.更改为3002后又重新工作,将其设置为5432.但是不知道为什么,因为如果端口已经被使用,那么节点应该抛出错误.

The problem for me was the port. It was set to 5432, after changing to 3002 worked again. Don't know why though, because if the port was already being used node should throw an error.

这篇关于"反应代理错误:无法将请求/api/从localhost:3000代理到http://localhost:5000(ECONNREFUSED)?错误..没有在线解决方案的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆