node日誌<script><alert>111</alert></script>
阿新 • • 發佈:2022-02-25
寫日誌 封裝和讀取
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 writeStreamView Code= fs.createWriteStream(fullFileName, { flags: 'a' }) return writeStream } // 寫訪問日誌 const accessWriteStream = createWriteStream('access.log') function access(log) { writeLog(accessWriteStream, log) } module.exports = { access }
const fs = require('fs') const pathView Code= require('path') const readline = require('readline') // 檔名 const fileName = path.join(__dirname, '../', '../', 'logs', 'access.log') // 建立 read stream const readStream = fs.createReadStream(fileName) // 建立 readline 物件 const rl = readline.createInterface({ input: readStream }) let chromeNum = 0 let sum= 0 // 逐行讀取 rl.on('line', (lineData) => { if (!lineData) { return } // 記錄總行數 sum++ const arr = lineData.split(' -- ') if (arr[2] && arr[2].indexOf('Chrome') > 0) { // 累加 chrome 的數量 chromeNum++ } }) // 監聽讀取完成 rl.on('close', () => { console.log('chrome 佔比:' + chromeNum / sum) })
安全
sql注入:竊取資料庫內容
xss攻擊:竊取前端的cookie內容
密碼加密:保障使用者資訊保安
npm i xss --save