本文介绍了通过node.js插入到没有任何模块的locale`json`文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我可以在没有任何模块的情况下将名称插入locale.json吗?

Can I Insert a name to locale.json without any modules?

这是我的json文件和我想在那里存储 john

This is my json File and I want to store john in That.

const locale = require('./locale.json');


推荐答案

你不能没有任何模块,但你可以使用 fs 等核心模块来做。但这并不像看起来那么容易。您需要读取文件,解析JSON,更新对象,再次序列化为JSON并编写文件,所有这些都正确处理错误并确保在写入过程中不会读取任何内容并且不会发生写入同时,处理竞争条件。不那么容易。很多锁定,同步和错误检查。有关类似问题的详细信息,请参阅此答案:

You cannot do it without any modules, but you can do it with core modules like fs. But this is not as easy as it may seem. You need to read the file, parse the JSON, update the object, serialize to JSON again and write the file, all that handling the errors correctly and making sure that nothing will get read while the write is in progress and that no writes will happen at the same time, handling race conditions. Not that easy. A lot of locking, synchronization and error checking. See this answer for more info to a similar problem:



  • Write JSON with query pseudo

这篇关于通过node.js插入到没有任何模块的locale`json`文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

11-02 15:58