嘿,所以我对编码很陌生,我已经学习JavaScript大约一个月了。我创建了一个非常基本的密码生成器,只是为了测试我学到的所有内容,但是我想弄清楚如何能够通过Node.js中的另一个脚本来存储这些密码。目前,我正在使用存储部分创建一个具有空ID和密码变量的对象,但是我不确定如何编写代码来让脚本创建多个对象来存储这些密码。如有任何帮助,将不胜感激!)
发布于 2022-02-19 15:50:43
由于他提到了JS和一般的编码,我认为我可以放心地假设OP不需要任何花哨的东西,只需要存储他生成的密码,不管在哪里。
当涉及到存储东西的时候,有很多的可能性。
一个很好的起点是首先寻找在文件中存储它们。
const fs = require('fs');
// say this is your passwords object
const passwds = myFunctionGeneratingPassword();
// a file 'passwords.json' will be created in your current working directory
const pathToFile = 'passwords.json';
try {
fs.writeFileSync(pathToFile, content)
//file written successfully
} catch (err) {
// if any error with the writing should occur, the error must be handled here
console.error(err)
}当然,您还需要朗读它们:
const fs = require('fs');
// the same file you created in the step before
const pathToFile = 'passwords.json';
try {
const data = fs.readFileSync(pathToFile, 'utf8');
const parsedData = JSON.parse(data);
console.log(parsedData);
} catch (err) {
// if any error with the reading should occur, the error must be handled here
console.error(err)
}现在,您还可以将它们存储在一个数据库中,比如MongoDB (还有许多其他的,MongoDB只是在JS堆栈中一般工作得很好),或者任何其他类型的文件,就像jfriend00在编写这篇文章时在注释中提到的那样,但是我认为在深入研究这些主题之前,您需要对JS做更多的修改。
https://stackoverflow.com/questions/71185941
复制相似问题