以下代码读取文件。
var filename = process.cwd() + "\\config\\test1.json";
var fs = require( 'fs' );
var contents = fs.readFileSync( filename,"utf8" );
console.log("contents");
var rulesObject = JSON.parse( contents );
test1.json具有以下内容
textpad
错误
contents
?textpad
undefined:1
textpad
^
SyntaxError: Unexpected token 
at Object.parse (native)
at readAccessRules (D:\s98799\config\resourceaccess.js:30:26)
at module.exports.accessRules (D:\s98799\config\resourceaccess.js:6:11)
at D:\s98799\config\resourceaccess.js:19:4
at Object.<anonymous> (D:\s98799\config\resourceaccess.js:40:7)
at Module._compile (module.js:456:26)
at Object.Module._extensions..js (module.js:474:10)
at Module.load (module.js:356:32)
at Function.Module._load (module.js:312:12)
at Module.require (module.js:364:17)
答案 0 :(得分:0)
var filename = process.cwd() + "/file.txt";
var fs = require( 'fs' );
var contents = fs.readFileSync( filename,"utf8" );
console.log(contents);
这将读取文件并将其记录下来(将file.txt更改为您的文件)