-1

I am trying to connect my client with the backend.

This is my code:

//client
const body = {
            email: value,
};
axios.get("http://localhost:5000/checkEmail", body)

//server.js
const playerRoutes = require("./routes/playerRoutes");

server.use(bodyParser.json());

server.use((req, res, next) => {
  res.setHeader("Access-Control-Allow-Origin", "http://localhost:3000"); //allow the access, * it allow from anywhere like codepen
  res.setHeader("Access-Control-Allow-Methods", "GET, POST"); //to allow the METHODS
  res.setHeader(
    "Access-Control-Allow-Headers",
    "Content-Type, Authorization",
    "Content-Type, apllication/json"
  ); //to allow types of Headers
  next();
});

//playerRoutes.js
const playerControllers = require("../controllers/playerControllers");

const router = express.Router();

router.post("/addPlayer", playerControllers.addPlayer);

router.get("/checkEmail", playerControllers.checkEmail);

//playerControllers
exports.checkEmail = async (req, res, next) => {
  console.log(req.body);
 }

why in req.body i get empty object {} ? I really can't solve the problem... can someone help me please?

Vash
  • 1
  • 1
  • A get doesn't have a body (...sort of, see e.g. https://stackoverflow.com/questions/978061/http-get-with-request-body), the second argument to the convenience method is the *options*. – jonrsharpe May 25 '20 at 11:45

1 Answers1

0

checkEmail is a GET request, what else were you expecting, if you want to send a body change it to a POST request.

Berk Kurkcuoglu
  • 1,453
  • 1
  • 9
  • 11