server – How to use Node.js to open, edit and save a text file from a webbrowser?
We are trying to manage a highscores file on the server. Every time a user sets a highscore it must be saved in a text file. But we are stuck with Node.js. From the command line using the node command on our own computer it is possible to open a file using the following javascript code, how to get it running in a webbrowser?
const fs = require('fs');
// Specify the file path
const filePath="c:/highscores.txt";
// Read the file asynchronously
fs.readFile(filePath, 'utf8', (err, data) => {
if (err) {
console.error('Error reading the file:', err);
return;
}
console.log('File content:', data);
});
We tried to import the Node.js module in an HTML script but the console replied it is not allowed to use a local module.
<!DOCTYPE html>
<html>
<head>
<title>File Reading and Writing</title>
<script type="module" src="https://stackoverflow.com/questions/77502757/C:\Program Files\nodejs\node_modules\npm\node_modules\postcss-selector-parser\dist\selectors\node.js"></script>
</head>
<body>
test!
</body>
</html>
Read more here: Source link