0

I was trying to send information from my client's site to my server, but on my server I am not receiving the information. I am using a FormData to send information to my server.

This is my code in my server:

import express from "express";
import bodyParser from "body-parser";

app.use(express.json());
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));

const startServer = () => {
  app.listen(8080, () => {
    console.log("server is running on port 8080");
  });
};
startServer();

this is my controller:


export const userRegister = async (req, res) => {
   
  console.log(req.body) // => {}

  try {
    const { name, imageProfile, description, password } = req.body;
    //insert user

    res.status(201).json({ success: true, data: user });
  } catch (e) {
    //error
  }
};

and from my client I send the information like this:

const Register = () =>{
  const dataForm = new FormData();
  dataForm.append("name", form.name);
  dataForm.append("description", form.description);
  dataForm.append("password", form.password);
  dataForm.append("imageProfile", form.imageProfile);

  UserRegister(dataForm).then(rst)
}

export const UserRegister = async (user) => {
  try {
    const response = await fetch(`${ENDPOINT}/user`, {
      method: "POST",
      body: user,
    });
    return response;
  } catch (error) {
    console.log(error);
  }
};

YCrhis
  • 165
  • 4
  • 14

0 Answers0