// node_modules\@types\node\fs.d.ts
// fs.readFile(fileName, callback);
// fs.writeFileSync(reportFilepath, reportHtml);
// fs.readFileSync( filePath, {
// encoding: 'utf8'
// } )
// fs.createReadStream("./index.html")
// fs.existsSync(dir)
// fs.mkdirSync(dir)
// write.sync( filePath, data )
const fs = require('fs');
// fs.createReadStream
// 首页路由
const router = new Router();
router.get("/", (ctx) => {
ctx.response.type = "html";
ctx.response.body = fs.createReadStream("./index.html");
});
// 目录不存在,如何创建它
const fs = require('fs');
const createDirIfNotExists = dir => (!fs.existsSync(dir) ? fs.mkdirSync(dir) : undefined);
// Example
createDirIfNotExists('test'); // creates the directory 'test', if it doesn't exist
// 获取某个文件中的某个参数
async function asyncAwaitTask() {
const { version } = fs.readFileSync('package.json');
console.log(version);
await Promise.resolve('some result');
}
// 正常版本的readFile(多参数版本)
fs.readFile(fileName, callback);
// Thunk版本的readFile(单参数版本)
var Thunk = function (fileName) {
return function (callback) {
return fs.readFile(fileName, callback);
};
};
var readFileThunk = Thunk(fileName);
readFileThunk(callback);
// 文件读写操作
var fs = require( 'fs' );
var write = require( 'write' );
var flatted = require( 'flatted' );
module.exports = {
tryParse: function ( filePath, defaultValue ) {
var result;
try {
result = this.readJSON( filePath );
} catch (ex) {
result = defaultValue;
}
return result;
},
/**
* Read json file synchronously using flatted
*
* @method readJSON
* @param {String} filePath Json filepath
* @returns {*} parse result
*/
readJSON: function ( filePath ) {
return flatted.parse( fs.readFileSync( filePath, {
encoding: 'utf8'
} ) );
},
/**
* Write json file synchronously using circular-json
*
* @method writeJSON
* @param {String} filePath Json filepath
* @param {*} data Object to serialize
*/
writeJSON: function ( filePath, data ) {
write.sync( filePath, flatted.stringify( data ) );
}
};
console