wzwd
wzwd

Reputation: 169

How to write file inside html of server using js?

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.

Upvotes: 0

Views: 107

Answers (1)

Gordon
Gordon

Reputation: 300

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.

Upvotes: 1

Related Questions