老师好,这段代码中我有个不明白的地方,我在nodejs文档中不确定writeStream.write(log + '\n')
里的.write
方法是被谁调用的
因为被return出来的writeStream
相当于是fs.createWriteStream(fullFileName, {flags: 'a'})
得到的结果,但是它为什么可以连接.write
呢?
是和fs.write()
方法有关吗?
还是和writable.write(chunk[, encoding][, callback])
这个方法有关?
const fs = require('fs')
const path = require('path')
// 写日志
function writeLog(writeStream, log) {
writeStream.write(log + '\n') // 关键代码
}
// 生成 write Stream
function createWriteStream(fileName) {
// 通过拼接路径找到真正的地址
const fullFileName = path.join(__dirname, '../', '../', 'logs', fileName)
const writeStream = fs.createWriteStream(fullFileName, {
flags: 'a'
})
return writeStream
}
// 写访问日志
const accessWriteStream = createWriteStream('access.log')
function access(log) {
writeLog(accessWriteStream, log)
}
module.exports = {
access
}