繁体   English   中英

我不断收到 POST https://localhost:5000/users/add.net::ERR_CONNECTION_REFUSED 错误。 我已经尝试过我在这里找到的建议修复,但没有任何效果

[英]I keep getting a POST https://localhost:5000/users/add net::ERR_CONNECTION_REFUSED error. I have tried suggested fixes i found here but nothing works

我正在使用 MERN 堆栈开发运动追踪器应用程序。 我有一个 React JS 组件,它允许我在按下提交按钮后向数据库添加一个新用户。 我正在使用 axios 将 http 请求从我的前端发送到后端的服务器端点。 但是我不断收到此错误

POST https://localhost:5000/users/add .net::ERR_CONNECTION_REFUSED 未捕获(承诺)错误:XMLHttpRequest.handleError (0.chunk.js:466) 处的 createError (0.chunk.js:971) 网络错误

这是我的服务器端代码

const express = require('express');
const cors = require('cors');
const mongoose = require('mongoose');
//mongoose is whats going to help us connect to our mongoDB database


require('dotenv').config();
//this configures si we can have our environment variables in the dotenv file

const app = express();
const port = process.env.PORT || 5000;
//this is how we will create our express server 

app.use(cors());
app.use(express.json());
//this is our middle ware this will allopw us to parse json
// cause the server will be sending s=and receiving json

const uri = process.env.ATLAS_URI;
mongoose.connect(uri, {useNewUrlParser: true, useCreateIndex: true, useUnifiedTopology:true});
//uri is where database is stored
const connection = mongoose.connection;
connection.once('open',() =>{
    console.log("MongoDB database connection established successfully");
});
//once connection is open its going to log the message

const exercisesRouter = require('./routes/excercises');
const usersRouter = require('./routes/users');
//importing 

app.use('/excercises',exercisesRouter);
app.use('/users',usersRouter);
//use files
//whenever somebody goes to route url and put /excersies at the end it will show
//everything in excercises and the same for users


app.listen(port,()=>{
    console.log('Server is running on port: ' + port);
});
//this is what starts the server. It start listening to a certain port

这是我提交的 function

onSubmit(e){
        e.preventDefault(); //prevents default html form behaviour taking place
        const user = {
            username: this.state.username,
        };

        console.log(user);

        //sending user data to the backend with post request
        //check user.js file in routes its sending a post request to the user.add api
        axios.post('https://localhost:5000/users/add',user)
            .then(res => console.log(res.data));

        this.setState({
            username: ''
        });
    }

这是我的路线

router.route('/add').post((req,res) => {
    const username = req.body.username;

    const newUser = new User({username});
    //using unsername to create new user

    newUser.save()
        .then(() => res.json('User added')) //after user saved to DB return user added message
        .catch(err => res.status(400).json('Error ' + err)); //if there is an error return error
});

检查您的后端是否也在端口 5000 上运行,您需要启动后端

我也跟着这个教程,你必须启动后端和前端。 问题是前端只是在运行,这就是为什么你可以看到一切(不确定他是如何管理的)但我不得不打开一个终端并使用 -> npm 启动前端,后端使用 -> nodemon server on a单独的终端选项卡

兄弟,我认为你的地址是 https://localhost:5000/users/add 而不是https将其更改为http它会解决你的问题你的地址将是 http://localhost:5000/用户/添加

暂无
暂无

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

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