Tengo un objeto json como este
{ APP_NAME: "Test App", APP_TITLE: "Hello World" }Ahora quiero convertir esto en un archivo javascript y descargar ese archivo, el formato de archivo debería ser así
// config.ts
export const APP_NAME: "Test App"; export const APP_TITLE: "Hello World";Para este caso, puede usar fs.createWriteStream() para escribir los datos en un archivo. Repita el objeto json y agregue el contenido.
Opción 1: lado del backend
// Initialize the file var fs = require('fs') var editor = fs.createWriteStream('config.ts') const data = { APP_NAME: "Test App", APP_TITLE: "Hello World" }; // Loop every keys Object.keys(data).forEach((key) => { // Append the text into the content editor.write(`export const ${key}: "${data[key]}";\n`) }); // Save everything and create file editor.end()Opción 2: lado frontal
<html> <script> const data = { APP_NAME: "Test App", APP_TITLE: "Hello World" }; let content = ''; Object.keys(data).forEach((key) => { // Append the text into the content content += `export const ${key}: "${data[key]}";\n`; }); let a = document.createElement('a'); a.href = "data:application/octet-stream,"+encodeURIComponent(content); a.download = 'config.ts'; a.click(); </script> </html>