[英]How to save javascript variable or console.log(variable) to a file
I am using node and I have some code that I run that puts the data from an API call out in a certain JSON format I need for dynamo. 我正在使用节点,我运行了一些代码,它将来自API调用的数据以我需要的特定JSON格式调用。 It is about 23000 records or so a day and I am attempting to save them to my hard drive.
它每天大约有23000条记录,我试图将它们保存到我的硬盘上。 Can someone please let me know how to save the output of a variable to a file?
有人可以让我知道如何将变量的输出保存到文件中吗? I have seen other pages on here but most of them seem to cover an html element with onclick or something along those lines.
我在这里看到过其他页面,但是大多数页面似乎都覆盖了带有onclick的html元素或类似的东西。 I appreciate any help.
我感谢任何帮助。
In node to write in a new file or replace old 在节点中写入新文件或替换旧文件
var fs = require('fs'); // reqire fileSystem node module
fs.writeFile("pathToFile", "Content", function(err) {
if(err) {
return console.log(err);
}
console.log("The file was saved!");
});
Or append to existing file 或附加到现有文件
fs.appendFile("pathToFile", "Content", function (err) {
if (err) throw err;
console.log('Saved!');
});
One of the most common ways to log something from JavaScript is to use AJAX to post the value of the JavaScript variable. 从JavaScript记录内容的最常见方法之一是使用AJAX发布JavaScript变量的值。 You can use any server-side script, including Node's FileSystem API to save a log of the variable to a specific file.
您可以使用任何服务器端脚本(包括Node的FileSystem API)将变量日志保存到特定文件。
Example: 例:
function saveVariable(variable) {
var xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200) {
alert("Saved");
}
};
xhttp.open("POST", "save?var=" + variable, true);
xhttp.send();
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.