skip to Main Content
import React from "react";
import client from "../../apis/client";

export default function SignUp() {
  const handleSignup = async (event) => {
    event.preventDefault();

    const username = document.getElementById('username').value;
    const email = document.getElementById('email').value;
    const password = document.getElementById('password').value;

    const userData = {
      username,
      email,
      password
    };

    try {
      const response = await client.post("/auth/signup", userData);
      console.log("Signup successful", response.data);
    } catch (error) {
      console.error("Error during signup:", error);
    }
  };

  return (
    <form id="signup-form" onSubmit={handleSignup}>
      <h1>Sign up</h1><br />

      <span className="input"></span>
      <input type="text" className="name" placeholder="Full name" required id="username" />
      <span className="input"></span>
      <input type="email" className="email" placeholder="Email address" required id="email" />
      <span id="passwordMeter"></span>
      <input type="password" className="password" id="password" placeholder="Password" />

      <button type="submit"><span>Sign up</span></button>
    </form>
  );
}
const router  =  require('express').Router();
const User = require('../models/User');




// Registering

router.post('/signup',async(req, res) =>{
    const newUser = new User(
        {
            username: req.body.username,
            email: req.body.email,
            password : req.body.password,
            isAdmin : req.body.isAdmin,

        },
    );
    const existingUser = await User.findOne({email:req.body.email})
        if(existingUser){
            return res.status(409).json({error:"user already exists"});
        }
    try{
        const user = await newUser.save();
        res.status(200).json(user);
    }catch(error){  
        res.status(500).json(error);        
    }
    
});



//Loginning In
router.post('/login',async(req,res) =>{
    try{
        const user = await User.findOne({email:req.body.email});
        !user && res.status(401).json('wrong password or username!')

    
        res.status(200).json(user)


    }catch(error){
        res.status(500).json(error)
    }
});

     

module.exports = router;

I’m trying to create the user in the mongoDB as I click the sign-up button. And The first paragraph of the ccode is from the front end (register page) and the code below are my back end api js code which contains post methods and etc

So could someone help with connecting the back end and the front end so i can use the post routes in the back and create a user in mongoDB

2

Answers


  1. Try running the backend code in a separate port and make the port accessible for the frontend.

    for example:
    if your react-app is running on http://localhost:3000
    and try to run your backend on some other port like http://localhost:8080

    use fetch() method to make the API call to the backend which is running on http://localhost:8080

    Login or Signup to reply.
  2. You can use fetch() to make API call. But i rather recommend using libraries like axios. axios has many builtin functions, which help making API calls more friendly for developer. For examples:

    • axios convert data in JSON automatically, while you have to read and parse data in JSON manually with fetch.
    • You can create a global axios instance and add things to it, which are used again and again, line Authorization Header or so.
    • axios has builtin XSRF(Cross-Site Request Forgery) protection

    And many more…..

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