1

Lets say i have two variables in my NodeJS front end Example.js file:

var a;
var b;

function senddata() {

       a = "Hassan";
       b = 7;

  }
return true;

Now what i'd like to do is send these two variables to my server using a POST request. At the backend i'm using ExpressJS for my routes.

In my Routes.js(At the backend) i want to receive the values of a and b through a POST request from my senddata() function.

In my routes i have created this route for the data:

router.post('/save', function (req, res, next) {


//Sending values of a and b to my database





});
sidius92
  • 21
  • 1
  • 3

1 Answers1

1

If you have NodeJs on the front-end too. I would suggest to use a http client, like Axios

Then you can use it this way:

const axios = require('axios');

function senddata() {

  axios.post('/save', {
    a: 'Hassan',
    b: 7
  })
  .then(function (response) {
      console.log(response);
  })
  .catch(function (error) {
      console.log(error);
  });
}
Sch-Tomi
  • 73
  • 7