message : “Request failed with status code 404” name : “AxiosError”

Issue

This Content is from Stack Overflow. Question asked by Miljana

I’ve been struggling to make the usersRoute work. I keep getting the error message: “Request failed with status code 404” name: “AxiosError”, get request is working but post not – Cannot POST /api/users/register. Could you help me figure this out, please? Thank you

usersRoute code is this:

const express = require("express");
const router = express.Router();
const User = require("../models/userModel");


router.post("/login", async(req, res) => {

const {username , password} = req.body

try {
    const user = await User.findOne({username , password})
    if(user){
        res.send(user)
    }
    else{
        return res.status(400).json(error);
    }
} catch (error) {
    return res.status(400).json(error);
}

});

router.post("/register", async(req, res) => {


try {
    const newuser = new User(req.body)
    await newuser.save()
    res.send('Korisnik uspešno registrovan')
} catch (error) {
    return res.status(400).json(error);
}

});

module.exports = router

userAction:

import axios from 'axios'
import {message} from 'antd'

export const userLogin=(reqObj)=>async dispatch=>{
    
    dispatch({type: 'LOADING', payload:true})

    try {
        const response = await axios.post('/api/users/login', reqObj)
        localStorage.setItem('user', JSON.stringify(response.data) )
        message.success('Prijava uspela')
        dispatch({type: 'LOADING', payload:false})
        setTimeout(() => {
            window.location.href='/'
            
        }, 500);
    } catch(error) {
        console.log(error)
        message.error('Pokušajte ponovo')
        dispatch({type: 'LOADING', payload:false})

    }

}

export const userRegister=(reqObj)=> async dispatch =>{
    
    dispatch({type: 'LOADING', payload:true})

    try {
        const response = await axios.post('/api/users/register' , reqObj)
        message.success('Registracija uspela')
        setTimeout(() => {
            window.location.href='/login'
            
        }, 500);
        window.location.href='/login'
        dispatch({type: 'LOADING', payload:false})
        
    } catch(error) {
        console.log(error)
        message.error('Pokušajte ponovo')
        dispatch({type: 'LOADING', payload:false})

    }

}

server.js:

const express = require('express')
const app = express()
const port = process.env.PORT || 5000
const dbConnection = require('./db')
app.use(express.json())


app.use('/api/cars/', require('./routes/carsRoutes'))
app.use('/api/users/', require('./routes/usersRoute'))
app.get('/', (req,res) => res.send('Hello World'))
app.listen(port, () => console.log(`Node JS Server Started in Post ${port}`))

userModel:

const mongoose = require("mongoose");
const userSchema = new mongoose.Schema({
    username : {type:String , required : true},
    password : {type:String , required : true}

})

const userModel = mongoose.model('users' , userSchema)
module.exports = userModel

package.json:

"proxy": "http://localhost:5000/"



Solution

This question is not yet answered, be the first one who answer using the comment. Later the confirmed answer will be published as the solution.

This Question and Answer are collected from stackoverflow and tested by JTuto community, is licensed under the terms of CC BY-SA 2.5. - CC BY-SA 3.0. - CC BY-SA 4.0.

people found this article helpful. What about you?