Tengo un archivo Node.js que contiene una función que escribe en archivos locales. Necesito llamar a esta función desde un archivo JavaScript del lado del cliente. ¿Cómo puedes hacer esto?
He visto preguntas sobre cómo llamar a funciones del lado del servidor en un evento HTML, pero no solo directamente desde un script del lado del cliente en cualquier momento.
Aquí está el script para escribir en el archivo local:
function writeCreateFile(filePath, newValue, callback) { checkIfFileExists(filePath, function(exists) { if(exists){ fs.writeFileSync(path.join(__dirname, filePath), newValue, 'utf-8') } else { fs.appendFileSync(path.join(__dirname, filePath),newValue); } callback(); }) } function checkIfFileExists(filePath, callback) { fs.readFile(filePath, 'utf-8', function(err, data) { callback(err); }); }