0

I want to pass data to an html and write it. I try something but it doesn't work. I don't know how to do it what I wanted to do.

I pass this file from router to and ejs file:

router.post("/home/addfiles/rightside", function(req,res){        
    var paragh = req.body.paragh ;
    res.render("files/writeto",{paragh:paragh}); 
)};

Then in my ejs file I want to write the file I pass from router using js:

   <div>      
         <p id="gethisformjs"> 

         </p>      
    </div>

 <script>
    var passeddata = <%- JSON.stringify(paragh)%>
      if (passeddata){
       // here I want to write the above (passeddata) to <p> </p> by finding its id
      }
 </script>

I can use this:

document.getElementById("gethisformjs").innerHTML = passeddata;

But the above dose not write it permanently to the server. I also try to use fs write method form JS but in this method I can overwrite all the file or write the file to new line, I couldn't write the file to specific location (find by id and write).

For this method I don't want to use database because I have limited space. How can I do it? any help please.

wzwd
  • 169
  • 2
  • 10
  • Your server JS and your browser JS are totally separate. [Have a read through](https://stackoverflow.com/questions/13840429/what-is-the-difference-between-client-side-and-server-side-programming) you'll need to re-think your program flow. – James Nov 11 '18 at 13:55
  • @james I try many way but it doesn't do what I want. For example I put all the html cods on server JS as text then I use fs write to write them to ejs file. It do the writing but it doesn't do things that I want. – wzwd Nov 11 '18 at 14:46
  • Let me just give you a friendly reminder about this problem. You need the `fs` module on your server side. Plus on your client side script, you need to submit the data to your server, preferably by using AJAX. – ionizer Nov 11 '18 at 17:45

1 Answers1

1

You are trying to use the front end Js to modify files on the backend. You can not modify the file system (eg html pages or anything on the client's computer) from within the browser. To do what you are trying to do, you need to pass the data from your page to your node application and have the node application perform the modification. Performing the modification will not be easy because you will have to do a text search of the file as you do not have the DOM on the backend (you can't do document.getElementByID(... and then set innerHTML). You will have to use the fs module to access the raw text of the html.

Gordon
  • 170
  • 3
  • 18
  • Thank you all. I understand that I can't do it in the way thought as in my question. But after some thinking I guess there will be another way to do it. I need to change my program flow to achieve what I plan. – wzwd Nov 11 '18 at 18:07