0

I have a location.hbs file where i have my script to get document.location and i am trying to pass document.location value to server.js file where i have done my express configurations. I want to post location details to my mongoDB.

code of my location.hbs file

<!DOCTYPE html>
<html>
<head>
  <title>My Express App</title>
  <script>
    // Send document.location to the server
    var locationValue = document.location.href;
    var xhr = new XMLHttpRequest();
    xhr.open('POST', '/location', true);
    xhr.setRequestHeader('Content-Type', 'application/json');
    xhr.send(JSON.stringify({ location: locationValue }));
  </script>
</head>
<body>
  <h1>Welcome to my Express App!</h1>
</body>
</html>

server.js

app.get("/",(req,res)=>{
    res.render("location");
  })

  app.post('/location', async(req, res) => {
    const location={
      location:req.body,
    }
    await user.insertMany([location])
    res.render("signup")
     
  });

0 Answers0