1200字范文,内容丰富有趣,写作的好帮手!
1200字范文 > 使用Node.JS 如何将JSON文件读入(服务器)内存?

使用Node.JS 如何将JSON文件读入(服务器)内存?

时间:2021-07-31 18:32:27

相关推荐

使用Node.JS 如何将JSON文件读入(服务器)内存?

本文翻译自:Using Node.JS, how do I read a JSON file into (server) memory?

Background背景

I am doing some experimentation with Node.js and would like to read a JSON object, either from a text file or a .js file (which is better??) into memory so that I can access that object quickly from code.我正在对Node.js进行一些实验,并希望从文本文件或.js文件(更好的??)中读取JSON对象到内存中,以便我可以从代码中快速访问该对象。I realize that there are things like Mongo, Alfred, etc out there, but that is not what I need right now.我意识到那里有Mongo,Alfred等东西,但这不是我现在所需要的。

Question题

How do I read a JSON object out of a text or js file and into server memory using JavaScript/Node?如何使用JavaScript / Node从文本或js文件和服务器内存中读取JSON对象?

#1楼

参考:/question/g0Jv/使用Node-JS-如何将JSON文件读入-服务器-内存

#2楼

Sync:同步:

var fs = require('fs');var obj = JSON.parse(fs.readFileSync('file', 'utf8'));

Async:异步:

var fs = require('fs');var obj;fs.readFile('file', 'utf8', function (err, data) {if (err) throw err;obj = JSON.parse(data);});

#3楼

Asynchronous is there for a reason!异步是有原因的!Throws stone at @mihai在@mihai扔石头

Otherwise, here is the code he used with the asynchronous version:否则,这是他用于异步版本的代码:

// Declare variablesvar fs = require('fs'),obj// Read the file and send to the callbackfs.readFile('path/to/file', handleFile)// Write the callback functionfunction handleFile(err, data) {if (err) throw errobj = JSON.parse(data)// You can now play with your datas}

#4楼

The easiest way I have found to do this is to just userequireand the path to your JSON file.我发现这样做的最简单方法是使用require和JSON文件的路径。

For example, suppose you have the following JSON file.例如,假设您有以下JSON文件。

test.jsontest.json

{"firstName": "Joe","lastName": "Smith"}

You can then easily load this in your node.js application usingrequire然后,您可以使用require在node.js应用程序中轻松加载它

var config = require('./test.json');console.log(config.firstName + ' ' + config.lastName);

#5楼

/dist/latest-v6.x/docs/api/fs.html#fs_fs_readfile_file_options_callback/dist/latest-v6.x/docs/api/fs.html#fs_fs_readfile_file_options_callback

var fs = require('fs'); fs.readFile('/etc/passwd', (err, data) => {if (err) throw err;console.log(data);}); // optionsfs.readFile('/etc/passwd', 'utf8', callback);

/dist/latest-v6.x/docs/api/fs.html#fs_fs_readfilesync_file_options/dist/latest-v6.x/docs/api/fs.html#fs_fs_readfilesync_file_options

You can find all usage of Node.js at the File System docs!您可以在文件系统文档中找到Node.js的所有用法!

hope this help for you!希望对你有所帮助!

#6楼

In Node 8 you can use the built-inutil.promisify()to asynchronously read a file like this在Node 8中,您可以使用内置的util.promisify()来异步读取这样的文件

const {promisify} = require('util')const fs = require('fs')const readFileAsync = promisify(fs.readFile)readFileAsync(`${__dirname}/my.json`, {encoding: 'utf8'}).then(contents => {const obj = JSON.parse(contents)console.log(obj)}).catch(error => {throw error})

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。