This is the first day of my participation in the August More text Challenge. For details, see: August More Text Challenge
1. Synchronous and Asynchronous:
Fs module A built-in module
const fs = require('fs')
Copy the code
Install other modules: NPM I
Custom module: (write js file) Node file name (.js can be omitted)
1. Asynchronous (callback)
var fs = require('fs')
fs.unlink('/tmp/shiyanlou'.function (err) {
if (err) {
throw err
}
console.log('/ TMP /shiyanlou successfully deleted')})Copy the code
2. Synchronization (no callback)
var fs = require('fs')
fs.unlinkSync('/tmp/shiyanlou') //Sync indicates the synchronization method
console.log('/ TMP /shiyanlou successfully deleted')
Copy the code
Note: No subsequent code can be executed until the synchronous method executes and returns the result
The asynchronous method uses a callback function to receive and return the result, allowing subsequent code to be executed immediately.
3. The flow is executed on the command line
var ws = fs.createWriteStream('./g.txt')
ws.write(str, 'utf-8')
ws.end()
ws.on('finish'.() = > {
console.log('Write successful')})Copy the code
ReadFile = readFile
Read file API
The fs.readFile(filename,[option],callback) method reads the file.
ReadFile synchronization: fs.readFileSync(filename,[options])
3. WriteFile
-
Read file API writeFile
Fs. writeFile(filename,data,[options],callback) Writes content to the file.
-
Create a server using HTTP
-
2.1 and display a sentence
Use the plug-in: Nodemon can listen for content changes and automatically restart the server when the content displayed in the output changes
-
2.2 Display a web page:
res.writeHead(200,{“Content-type”:”text/html; charset=utf-8″});
-
2.3 Creating and starting a service:
-
ser.js
const http = require('http')
const fs = require('fs')
const data = require('./data')
var ser = http.createServer((req, res) = > {
res.writeHead(200, { 'Content-type': 'text/html; charset=utf-8' })
var nr = fs.readFileSync('./a.html'.'utf-8')
nr = nr.replace(/\{name\}/, data.name)
nr = nr.replace(/\{age\}/, data.age)
res.end(nr)
})
ser.listen(2100.() = > {
console.log(`listen ${ser.address().port}. This is my port)})Copy the code
Request file priority:.js.json.node
- Route: Path input different content request different pages
const http = require('http')
const fs = require('fs')
const path = require('path')
const data = require('./data') // Request data data.json
var ser = http.createServer((req, res) = > {
console.log(req.url)
var extname = path.extname(req.url)
// Obtain the path of the request file
// The requested file is in the same directory as the ser.js file. The requested a.HTML file will be referenced normally, and the ser.js req. Url will automatically find the path of the file to be referenced from the HTML file
var filename = '/' + req.url
if (req.url === '/favicon.ico') return
if (req.url === '/') {
console.log('/ request')
res.writeHead(200, { 'Content-type': 'text/html; charset=utf-8' })
var nr = fs.readFileSync('./a.html'.'utf-8')
nr = nr.replace(/\{name\}/, data.name)
nr = nr.replace(/\{age\}/, data.age)
res.end(nr)
} else if (req.url === '/two') {
res.writeHead(200, { 'Content-type': 'text/plain; charset=utf-8' })
res.end('This is two request, two request.')}else if (req.url === '/aia') {
res.writeHead(200, { 'Content-type': 'text/plain; charset=utf-8' })
res.end('This is the moment of the beautiful half-immortal.')}else if (extname === '.css') {
res.writeHead(200, { 'Content-type': 'text/css' })
fs.createReadStream(filename).pipe(res)
} else if (extname == '.png') {
res.writeHead(200, { 'Content-type': 'image/png' })
fs.createReadStream(filename).pipe(res)
} else {
res.writeHead(200, { 'Content-type': 'text/plain; charset=utf-8' })
res.end('Is my rosy glow')
}
})
ser.listen(2100.() = > {
console.log(`listen ${ser.address().port}. This is my port)})Copy the code
Refueling forward
On the way to study, it is doomed to be lonely and dull, waiting for you to break the cocoon into a butterfly! Come on!