This is the 29th day of my participation in Gwen Challenge
Go back on your word
Originally thought that the Node series to temporarily say goodbye, ready to do deployment, login and other functions after the completion of the interface, ready to deploy online, in the last article also slightly summarized, the results found that there is no log, log behind the security, although it is only the first step… Log like login, is also a big module, it takes a lot of time to learn, don’t say, take your time, can’t hurry ~
Log profile
- Access log Axcess log
- Custom logging (including custom events, error logging, etc.)
File reading and writing Demo
const fs = require('fs')
const path = require('path')
const fileName = path.resolve(__dirname, 'data.txt')
// Read the contents of the file
// fs.readFile(fileName, (err,data) => {
// if(err) {
// console.log(err);
// return
/ /}
// console.log(data.toString());
// })
// Write to the file
const content = 'This is the new entry \n'
const opt = {
flag: 'a' // a: append, w: write, overwrite
}
fs.writeFile(fileName, content, opt, err= > {
if (err) {
console.log(err)
}
})
Copy the code
Stream
demo
/* * @Author: Tmier * @Date: 2021-06-29 23:04:28 * @LastEditTime: 2021-06-29 23:40:41 * @Description: * @LastModifiedBy: Tmier */
// Standard input/output
// process.stdin.pipe(process.stdout)
// const http = require('http')
// const server = http.createServer((req,res) => {
// if(req.method == 'POST') {
// req.pipe(res)
/ /}
// })
// server.listen(8000)
// Copy the file
// const fs = require('fs')
// const path = require('path')
// Two file names
// const fileName1 = path.resolve(__dirname, 'data.txt'
// const fileName2 = path.resolve(__dirname, 'data-bak.txt')
// Read the stream object of the file
// const readStream = fs.createreadstream (fileName1) // create a readStream
// Write the stream object to the file
// const wirteStream = fs.createwritestream (fileName2) // createWriteStream
// Execute the copy through pipe
// readStream.pipe(wirteStream)
// The data is read, that is, the copy is completed
// readstream. on('data', chunk => {// listen for streams
// console.log(chunk.toString());
// })
// readStream.on('end', () => {
// console.log('copy done! ');
// })
// Combine file IO with network IO
const http = require('http')
const fs = require('fs')
const path = require('path')
const fileName1 = path.resolve(__dirname, 'data.txt')
const server = http.createServer((req,res) = > {
if(req.method == 'GET') {
const readStream = fs.createReadStream(fileName1)
readStream.pipe(res)
}
})
server.listen(8000)
Copy the code
Several small demos above have finished reading and copying files, and have a more in-depth understanding of the flow. If I have time tomorrow, I will start to write interface logs, about three: Access. Log, error. Log, event. Log.