Skip to content

Instantly share code, notes, and snippets.

@wuchengwei
Created May 2, 2012 02:50
Show Gist options
  • Save wuchengwei/2573247 to your computer and use it in GitHub Desktop.
Save wuchengwei/2573247 to your computer and use it in GitHub Desktop.
NodeJS - Read in a json file asynchronously
/*
* @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);
}
}
});
}
@WreckRalph
Copy link

hello
如果我用koa2框架 koa-body去读取json文件,如何读取到json的内容呢?
const file = ctx.request.body.files;
类似这种我只能读取到json文件,但是并没有看到文件中的内容?
麻烦请教一下

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment