0

So I've been searching for hours on this topic, and still have not found any real way to achieve what I'm trying to do here.

Simply, I just want to write some text to a text file through the use of a button in HTML. This is what I have:

<!DOCTYPE html>
<html>
<head>
    <title>TEST</title>
</head>
<body>

<button onclick="write_text()">WRITE FILE</button>

<script>
    function write_text(){
        var fs = require('fs');

        fs.writeFile("test.txt", "okay this is epic", function(err){
            if (err) return console.log(err);
            console.log("Nice");
        });
    };
</script>

</body>
</html>

I'm unsure as to why this doesn't work, do I need to make a separate .js file for the function that the button references?

Any help would be appreciated, Thanks.

EDIT: I'm trying to save the file to my GoDaddy server so that I can access it later, not just download the file. Testing it locally, it should create a file in the directory of my html document.

2
  • 3
    HTML runs in the browser. Node.js runs on the server. Commented Sep 6, 2018 at 1:19
  • 3
    You can't. Node is server side, HTML client side. Make sure you understand the concept. Commented Sep 6, 2018 at 1:19

2 Answers 2

1

As was stated before, you do not do it right. it is very important to say that node.js is a runtime environment, and simply putting an HTML file with JS code on GoDaddy does not make it "server-side", since the code runs on the browser and not on the server.

What you really want to do is either using Blob as stated before or doing something like this (if you want to use node.js):

var express = require('express')
var app = express()

app.post('/<your_path>', function (req, res) {
  writeToFilexx(/*Here you may want to pass data using body parser*/)
})
/* 

Here you will start the server 

*/

Please note, writeToFilexx is a function you have to implement using fs. on the client side, you will have to send the server a request with the data you want to write to a file. It goes like this: 1. client sends data to the server. 2. server gets data (handles the request). 3. server process, and stores data.

As you have probably guessed, the file will be saved on the server, not on client's PC.

Sign up to request clarification or add additional context in comments.

Comments

0

You can't do it in the way you want (not server side, no node, then no fs in this context), but there is a workaround creating a blob:

function downloadURL(url, name) {
  var link = document.createElement("a");
  link.download = name;
  link.href = url;
  document.body.appendChild(link);
  link.click();
  document.body.removeChild(link);
  delete link;
}

function downloadFile() {
  var data = "okay this is epic";
  var blob = new Blob([data], {type: 'text/txt'});
  var url  = window.URL.createObjectURL(blob);
  downloadURL(url, "test.txt");
}

Call downloadFile() in your button.

2 Comments

I see... But the thing is I'm trying to figure out how this would save to the file directory of the server that is hosting my website, so that I can access it later.
Got it. Well, you really need to understand the concepts, mainly how client and server communicates and their roles. Not telling you here, cause it is a long history... but trying to simplify, the client will make a request (http, for example) to the server to save the file for you, then there you use fs.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.