Created
May 2, 2012 02:50
-
-
Save wuchengwei/2573247 to your computer and use it in GitHub Desktop.
NodeJS - Read in a json file asynchronously
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* @param {String} filepath | |
* @param {function} callback function(err, data){} | |
*/ | |
function readJsonFileAsync(filepath, callback) { | |
var fs = require('fs'); | |
fs.readFile(filepath, 'utf-8', function(err, data) { | |
if (err) { callback(err, null); } | |
else { | |
result = JSON.parse(data); | |
if (result) { | |
callback(null, result); | |
} else { | |
callback('parse error', null); | |
} | |
} | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
hello
如果我用koa2框架 koa-body去读取json文件,如何读取到json的内容呢?
const file = ctx.request.body.files;
类似这种我只能读取到json文件,但是并没有看到文件中的内容?
麻烦请教一下