什么是nodenode.js是一个异步事件驱动的JavaScript运行时node特性非阻塞I/O事件驱动的公共模块fsreadFile(),异步读取文件fs.readFile('./config.js',(err,data)=>{if(err)throwerrconsole.log(data)})readFileSync(),同步读取文件,会造成阻塞效果constdata=fs.readFile('./config.js')//代码会此处阻塞console.logpathpath和fs经常一起使用resolve()将一系列路径或路径片段解析为绝对路径constpath=require('path')console.log(path.resolve(_dirname,'./02.js'))//D:\mytest\node\01\02.jsconstdata=fs.readFile(path.resolve(__dirname,'./03-http.js'),(err,data)=>{console.log(data.toString())})relative(from,to)返回from到to的相对路径constpath=require('path')console.log('01','02.js')//..\mytest\node\01\02.js目录结构为D:\mytest\node\01\02.jsutilpromisify将异步方法封装成返回promise实例的方法constreadFile=promisify(fs.readFile)readFile('./conf.js').then(data=>console.log(data))promisesconstfsp=require('fs').promisesfsp.readFile('./config.js').then(data=>安慰。log(data)).catch(err=>console.log(err))httpconsthttp=require('http')constserver=http.createServer((request,reponse)=>{response.~~~~end('hahahah')})server.listen(3000)
