English 中文(简体)
后向前端连接
原标题:Connecting Back-end to Front-end
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;

当我点点起 sign子时,我试图在蒙戈亚行制造用户。 而且 密码的第一段是前端(登记页),下面的代码是我后面的一部载有员额方法和其他方法的皮条码。

因此,有人可以帮助将后端和前端连接起来,这样就能够使用后方的邮政路线,并在蒙戈亚行建立用户。

问题回答

在一个单独的港口操作背书代码,使港口能够进入前线。

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

使用fetch() 方法,使APIC打电话给背稿,网址是:http:// localhost:80





相关问题
jqGrid dynamic select fields

is there a way to modify the values of a status field in the colModel dynamically? Lets say we have a col Model with a field like: ... field ... name: "state",type: "select", editoptions: {value: "0:...

Changing an Object s Type in JavaScript

I have an array of existing object defined with JSON. The objects are obviously of the Object type. How do I associate them with a custom object type to give them specific functionality?

Disappearing object function?

Is there a reason for object functions to be unset or deleted or simply not applied for any reason at all that isn t intentional? I am maintaining someone elses code and gone through it many times. I ...

JS: Object iteration der

在我的联合材料中,我有一个称为方框。

Packing tree into array in Javascript

Ok, so this shouldn t be difficult, however I have encountered weird and bizarra flukes. I am trying to pack a tree into an array, where each node is something like: title: string-for-display key: ...

热门标签