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 }); 
0

1 Answer 1

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); }); } 
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.