skip to Main Content

I am having some problem while to connecting to end point on server.

Here is my code.

Register.jsx

 function Register() {
  const [registerUser, setRegisterUser] = useState({
    username: "",
    email: "",
    password: "",
  });
  const handleClick = async (e) => {
    try {
      e.preventDefault();
      const response = await RegisterUser(registerUser);
      // some code 
  };
}

users.js

import axios from "axios";

export const RegisterUser = async (registerUser) => {

  console.log("In RegisterUser =====> ", registerUser);
  try {
    const response = await axios.post("/api/users/register", registerUser);
    return response.data;
  } catch (error) {
    return error.response.data;
  }
};

server.jsx

const express = require("express");
const cors = require("cors");
require("dotenv").config();
const app = express();
const dbConfig = require("./Config/dbConfig");
const port = process.env.PORT || 5000;

const server = require("http").createServer(app);

const usersRoute = require("./routes/usersRoutes");

app.use(cors({ origin: "http://localhost:3000" }));

app.use("/api/users", usersRoute);

server.listen(port, () => console.log(`Server running on port ${port}`));

usersRoutes.js

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

// User Registration
router.post("/register", async (req, res) => {
  console.log("In /register");
  try {
    const user = await User.findOne({ email: req.body.email });
    if (user) {
      return res.send({
        success: false,
        message: "User already exists",
      });
    }
    const newUser = new User(req.body);
    await newUser.save();
    res.send({
      success: true,
      message: "User created successfully",
    });
  } catch (error) {
    res.send({
      message: error.message,
      success: false,
    });
  }
});

module.exports = router;

When i submit my registration form I want it to send it post request to the url of the api and recieve its response.But I am getting error and it seems that, there is a problem with end point.

I am getting following error for above code :

POST http://localhost:3000/api/users/register 404 (Not Found)

2

Answers


  1. From your error message, either your server is not running or you are running it on a different port. Either way, Axios cannot see your server. Are you using the same folder for both your server and client side? If so, I suggest you separate them so that you can run the server side independently from the client. This way, you can run the server independently and use it from the client.

    Login or Signup to reply.
  2. Looks like a ports issue.
    In the cors you had mentioned the origin http://localhost:3000, also in the Axios URL it is http://localhost:3000/api/users/register using port 3000. 2 applications cannot run on a single port, the thing you are trying is running both frontend and backend on port 3000, I don’t see the port mentioned in env but if it is not 3000 then, in that case, you have to change your Axios URL port number to that port.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search