简体   繁体   English

尝试使用Node fs.writeFile写入json文件

[英]Trying to write to a json file using Node fs.writeFile

I hope I'm saying this correctly. 我希望我说的正确。 What I'm trying to do is write to a json file using fs.writeFile. 我正在尝试使用fs.writeFile写入json文件。

I can get it to work using the command line but what I want to do is call a function maybe a button click to update the json file. 我可以使用命令行来使其工作,但是我想做的是调用一个函数,或者单击按钮以更新json文件。

I figure I would need some type of call to the node server which is local port 8080. I was researching and seen somebody mention using .post but still can't wrap my head around how to write the logic. 我认为我需要对本地端口8080的节点服务器进行某种类型的调用。我正在研究,发现有人提到使用.post,但仍然无法理解如何编写逻辑。

$(".button").on("click", function(event) {

    fs.writeFile("./updateme.json", "{test: 1}", function(err) {
        if(err) {
            return console.log(err);
        }
        console.log("The file was saved!");
    });

});

Using jQuery along with fs ? 使用jQuery和fs吗? Wow that could be great! 哇,太棒了! Unfortunately that is not as simple as that! 不幸的是,事情并非如此简单!

Let me introduce you to server-side VS client-side JavaScript . 让我向您介绍服务器端VS客户端JavaScript Well actually there are a lot of resources on the net about that - just google it, or check the answers to this other StackOverflow question . 嗯,实际上,网络上有很多相关资源-只需将其谷歌搜索,或查看其他StackOverflow问题的答案即可。 Basically JavaScript can run either on a browser (Chrome, Mozilla...) or as a program (usually a server written in NodeJS), and while the language is ( almost ) the same, both platforms don't have the same features. 基本上,JavaScript既可以在浏览器(Chrome,Mozilla ...)上运行,也可以作为程序(通常是用NodeJS编写的服务器)运行,并且尽管语言( 几乎 )相同,但是两个平台却没有相同的功能。

The script that you're showing should run in a browser, because it's using jQuery and interacting with buttons and stuff (aka the DOM ). 您显示的脚本应该在浏览器中运行,因为它使用的是jQuery并与按钮和内容(也称为DOM )进行交互。 Can you imagine what a mess it would be if that script could interact with the file system? 您能想象如果该脚本可以与文件系统进行交互将是一团糟吗? Any page you'll visit will be able to crawl around in your holiday pictures and other personal stuff you keep on your computer. 您将访问的任何页面都将能够在您的假日图片和您保存在计算机上的其他个人资料中爬行。 Bad idea! 馊主意! That is why some libraries like fs are not available in the browser. 这就是为什么浏览器中不提供fs类的库的原因。

Similarly, some libraries like jQuery are not available (or simply useless) in the server, because there is no HTML and user interaction, only headless programs running. 同样,由于没有HTML和用户交互,只有无头程序在运行,因此jQuery之类的某些库在服务器中不可用(或根本没有用)。


So, what can I do to write a JSON file after a user clicks on a button? 那么,在用户单击按钮之后我该怎么做才能写入JSON文件?

You can set up: 您可以设置:

  • A NodeJS server that will write a JSON file 将写入JSON文件的NodeJS服务器
  • Make jQuery call this server with the data to be written after the user clicks on a button 在用户单击按钮后,使jQuery使用要写入的数据调用此服务器

If you want further guidelines on this, tell me in the comments! 如果您需要进一步的指导,请在评论中告诉我! I'll be ready to edit my question so as to include instructions on setting up such an environment. 我将准备编辑我的问题,以包括有关设置此类环境的说明。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM