最近一直在学习node.js,所以总结了一些笔记记录在这里。modulesmodulesmodulesandscopenodeffferFileHttpstreammodularizationandscopethis在浏览器中this指的是globalthis在windownode脚本文件中没有定义时指向global,定义时module.exportsa=10console。log(global.a)//10全局作用域没用。用varletconst定义的声明都是全局作用域。模块范围用变量修饰。它们都是模块作用域。Functionscopesblock-levelscopesBufferhandlesbinarydata//数组拼接缓冲区rs=require('fs').createReadStream('test.txt',{highWaterMark:10})vardata=[]rs.on('data',function(chunk){data.push(chunk)})rs.on('end',function(chunk){varbuf=Buffer.concat(data)console.log(buf.toString())})FileSystemreadFile用于异步读取文本文件的内容,会将一个文件的所有内容读入内存,适用于小文本文件;读取大文件,使用stream,readFileSync直接返回文本数据内容readFile第一个参数为文件名,如果没有自动返回create。fs.stat获取文件的状态通常开发者可以在调用open()read()或write方法之前调用fs.stat方法来判断文件是否存在。fs.stat和fs.fstat的作用是一样的,区别在于fstat方法的第一个参数是文件描述符,格式为整型,fstat方法通常和open方法一起使用,因为结果open方法返回的是一个文件描述。varfs=require('fs')vardata=fs.readFileSync('test.txt',{encoding:'utf-8'})console.log(data)fs.writeFile('foo.txt',"你好world",{flag:'a',encoding:'utf-8'},function(err){if(err){console.log(err)return}console.log('success')})fs.stat//用于获取文件状态,判断文件是否存在//经常在调用openreadwiter时,调用fs.stat("foo.txt",function(err,result){if(err){console.log(err)return}console.log(result)})//fs.fstatfs.open("foo.txt",'a',function(err,fd){if(err){console.log(err);返回;}console.log(fd);fs.fstat(fd,function(err,result){if(err){console.log(err);return;}console.log(result);})})//示例//获取目录中的文件名。//fs.readdir和fs.stat两个apivarsfs=require("fs");functiongetAllFileFromPath(path){fs.readdir(path,function(err,res){for(varsubPathofres){varstatObj=fs.statSync(path+"/"+subPath);//这里使用了同步的方式,而不是异步的if(statObj.isDirectory()){//判断是否是文件夹console.log("Dir:",subPath);getAllFileFromPath(path+"/"+subPath)//如果是文件夹,则递归获取子文件夹中的文件列表}else{console.log("File:",subPath);}}})}getAllFileFromPath(__dirname);httpvarhttp=require('http')varserver=http.createServer(function(req,res){//处理http请求varmethod=req.methodvarurl=req.urlconsole.log(method,url)res.writeHead(200,{'content-type':'text/plain'})res.end('hellonode!')})//监听来自客户端的事件server.on('connection',function(req,res){console.log('connection')})server.on('request',function(req,res){console.log('request')})server.listen(8080)//访问8080时,控制台进入//已连接//请求//请求被打印两次,因为其中一次是对favicon.ico的请求//一个简单的静态服务器varhttp=require('http')varfs=require('fs')varserver=http.createServer(function(req,res){if(req.url=='/'){//access8080varfileList=fs.readdirSync('./')res.writeHead(200,{'content-type':'text/plain'})//将数组转换为字符串并返回res.end(fileList.toString())}else{varpath=req.url;//在路径字符串前加上.表示当前目录,避免在nix系统中访问/folderfs.readFile('.'+path,function(err,data){if(err){res.end('doesnotexist')throwerr;}res.writeHead(200,{'content-type':'text/plain'})res.end(data)})}})//1buffer.js,2http.js,3httpexmple.js,4.js,foo.txt,login.html,md.md,readFile.js,test.txt,this.js,upload.jsserver.listen(8080)//处理异常process.on('uncaughtException',function(){console.log('goterror')})processhttprequestmethodurlheadergetpostputdeleteupdate//处理http请求varmethod=req.methodvarurl=req.urlResponseobjectuploaddataupload.html<formaction="/upload"method="post"enctype="multipart/form-data">
