How to pass the value of a client variable to a Node.JS server?

Asked

Viewed 14 times

0

I am developing an application that will need to replace an XML via FS, and all the contents of that XML is stored in a variable that is created by JS in the client after pressing a button. I need to get the value of this variable from within the Node.JS server, to use as a parameter for the function writeFile() from FS. How do I do this?

Client JS:

 let xml=(`
 <?xml version="1.0" encoding="UTF-8"?>
 <Exemplo>
  <Titulo>Hello World</Titulo>
 </Exemplo>
`)

Server JS:

const server = http.createServer((request,response)=>{
 fs.writeFile('dataset.xml',QUERO_A_VARIAVEL_AQUI,function(err){
  if(err) return console.log(err);
 })
})

I have tried using import and export, but got errors that variable cannot be accessed after startup. I tried to play the function of generating XML inside the Server.JS file and calling it in HTML being activated by onclick="" button, but still no value is accessed by the server, and if I put the function inside the server it is as if it simply did not exist.

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.