A, Node.js basics
1, the concept of
- Simply put Node.js is running on the server side JavaScript.
- Node.js is a JavaScript runtime environment
Node.js uses an event-driven, non-blocking I / O model, making it lightweight and efficient.
2, installation
Download the official website address: http: //nodejs.cn/
nodeJS5 basic objects:
- 1, require the introduction of module
- 2, export export target
- grammar:
1)export.属性 = 值; 2)export.方法名 = 函数;
- note:
1)export时module对象的引用 export == module.export (指向同一个内存空间) 2)export是module.export的引用, 不能改指向,只能添加属性和方法 3)module.export才是真正的暴露对象,指向哪里就暴露哪里-----推荐使用
- 3, module module object
module.export module.export.属性 = 值; module.export.方法名 = 函数; module.export = 对象或函数 module.id 模块id,模块名称 module.parent 模块父级 module.filename 模块文件名和路径 module.children 子模块列表 module.paths 模块查找路径,如果当前目录找不到node_modules就去上一级目录找,直到根目录
- 4, __ filename of the current absolute path js file
5, __ current js file folder absolute path where the file dirname
npm package manager (node Package Manager)
- package.json is node.js project description file that describes the project as json format
- Creating package.json file ----> npm init npm init -y ----> automatic creation of all yes
- package.json common attributes
name: 项目名称 version:版本号 description:项目描述 main:主模块 dependencies:项目依赖 devDependencies :开发时依赖 scripts:脚本命令,可以使用npm命令执行 license:开源协议
npm common commands:
npm install <包的名称> i--->install npm i <包的名称>@版本号 //安装指定版本 npm i <包的名称> -g全局安装 -S(save)写入项目依赖列表 -D(dev)写入开发依赖列表 npm search <包的名称> //搜索包 npm view <包的名称> //查看包 npm uninstall <包的名称> //卸载包 npm update <包的名称> //更新包
cnpm (Taobao Mirror)
asl install -g cnpm --registry = https: //registry.npm.taobao.org
nodeJS callback function
- Callback mechanism:
- a., define a normal function
- b. The function passed as a parameter to another function (the caller)
- c. The caller decision based on the timing and conditions during execution if the calling function
- The callback function uses:
- Usually used when the condition reaches a certain opportunity or need to execute code using a callback function
Synchronous and asynchronous
- Synchronization: After the completion of the implementation of the previous line, the next line to be implemented
- Asynchronous: the more complex tasks in order to achieve the task threads, without waiting for the completion of an execution, the next one can be executed.
- Three kinds of asynchronous implementations:
- (1) callback function
callback function is not necessarily asynchronous (forEacch), asynchronous callback function must have - (2) events (for server-side event)
event sources .on ( 'Event Name', the callback function)
/* 开启一个服务器*/ var http = require('http'); // 建立服务器 var app = http.createServer(function(request, response) { response.writeHead(200, { "Content-Type": "text/plain" }); response.end("Hello world!"); }); //启动服务器 app.listen(80,function(){ console.log('服务器已运行') })
- (3) promise commitment Objects
/* 什么是promise? promise是es6中新增的承诺对象,用于对异步的操作进行消息的传递 promise的状态? Pending 等待中 Resolved 成功 Rejected 失败 Pending => Resolved Pending => Rejected promise 有什么用? promise可以传递异步消息 由于异步的返回结果时间顺序不可控,所以需要使用promise来统一控制输出结果 */ var promise = new Promise(function(resove,reject){ resolve() }) //调用对象 promise.then(res>{ //成功的回调 }).catch(err=>{ //失败的回调 }) //利用promise对象的all方法可以实现手动调整输出顺序,相当于把异步变为同步 Promise.all([p1,p2]).then(datas=>{ //返回数组 })
- (1) callback function
Two, Buffer cache, and the file module
1, Buffer cache
concept
Opened up a staging area in memory for storing operation we need to bytecode
Create a buffer zone
- Create a specified length buffer
var buf = new Buffer(大小) //创建5个字节的缓存区
buf.write('a') //存入一个字节 转成16进制 的Ascall码的61 在node中默认使用utf-8编码,一个中文3个字节
- Create a buffer in the specified array coding
var buf = new Buffer([十进制编码]) //数字小可以
- Create a character buffer specified
var buf = new Buffer('字符串')
Write buffer
buf.write('字符串')
Reading cache
buf.toString()
Copy buffer
buf.copy(buf2)
2, module file (fs)
Read the file
- Since nodejs is the server program, you must have read and write files in the client does not have such a function
- File read and write in two ways:
- Direct reading:
- Everything on the hard disk only trigger callback function after all read into memory
- Two way:
异步:定义一个回调函数,接收读取到的内容 fs.readFile('文件路径',(err,data)=>{}) 同步:几乎所有fs的函数都有同步版本,只需在异步版本后面加Sync即可 (Async:异步) fs.readFileSync('文件路径')
- Streaming read:
The data read from a hard disk triggers a callback function, to achieve a large file operations
Write file
- Direct reading:
- Synchronized version
fs.writeFileSync('文件名','数据')
- Asynchronous version
fs.writeFile('文件名','数据',funciton(err){/*写完文件以后执行的代码*/})
Reads the file information
fs.stat('文件名',function(err,state){
//state时文件信息对象,包含了常用的文件信息
//size: 文件大小,单位字节
//mtime: 文件修改时间
//birthtime 文件创建时间
//方法
.isFile() //判断当前查看的对象是不是一个文件
.isDirectory() //判断是不是一个目录
})
Delete Files
fs.unlink('文件名',function(err){})
Demand: Enter code implements delete a non-empty directory
- Delete empty directories
fs.rmdir()
- Read files in the directory and folder list
fs.readdir()
- Read the details of each folder
fs.stat()
- If it is judged file
fs.unlink()
- If the judgment is a directory
//递归调用自己
- Delete empty directories
fs.rmdir()
Stream reader
- Flow: What is the flow
- All Internet data are streamed, a group of flow starting with a data transmission end
- Flow of operation:
- Streaming read the file
//可读取数据的流 var fs = require("fs"); var data = ''; // 创建可读流 var readerStream = fs.createReadStream('input.txt'); // 设置编码为 utf8。 readerStream.setEncoding('UTF8'); // 处理流事件 --> data, end, and error readerStream.on('data', function(chunk) { data += chunk; }); readerStream.on('end',function(){ console.log(data); }); readerStream.on('error', function(err){ console.log(err.stack); }); console.log("程序执行完毕");
- Streamed write file
//可写入数据的流 var fs = require("fs"); var data = 'hello world'; // 创建一个可以写入的流,写入到文件 output.txt 中 var writerStream = fs.createWriteStream('output.txt'); // 使用 utf8 编码写入数据 writerStream.write(data,'UTF8'); // 标记文件末尾 writerStream.end(); // 处理流事件 --> data, end, and error writerStream.on('finish', function() { console.log("写入完成。"); }); writerStream.on('error', function(err){ console.log(err.stack); }); console.log("程序执行完毕");
Flow conduit
pipe provides a mechanism for outputting the input flow stream. We typically used to obtain the data from one data stream to another stream passing
`` `JavaScript
var FS = the require (" FS ");// create a stream-readable
var readerStream = fs.createReadStream ( 'input.txt' );// Create a writable stream
var writerStream = fs.createWriteStream ( 'output.txt' );// write pipeline operation
console.log ( "program is finished");
// Read input.txt file content, and the content is written to a file output.txt
readerStream.pipe (writerStream);
`` `- Chain Flow
//压缩文件 var fs = require('fs'); var zlib = require('zlib'); // 压缩 input.txt 文件为 input.txt.gz fs.createReadStream('input.txt') .pipe(zlib.createGzip()) .pipe(fs.createWriteStream('input.txt.gz')) console.log("文件压缩完成。"); //解压文件 var fs = require("fs"); var zlib = require('zlib'); // 解压 input.txt.gz 文件为 input.txt fs.createReadStream('input.txt.gz') .pipe(zlib.createGunzip()) .pipe(fs.createWriteStream('input.txt')); console.log("文件解压完成。");
Third, the common web crawler module
1, common module
path module
- Formatting path
path.nomalize(p)
- Stitching path (a plurality of the strings together into a complete path)
/*使用path.jon拼接文件路径和 连接符 拼接优点
1.自动帮我们添加路径分隔符(根据当前操作系统)
2.自动改正错误的路径分隔符
*/
path.join(path1,path2)
let url = path.join(__dirname,path1); //常用
- Part of the return path folder
path.dirname(p)
- Part of the return path file (the file name and extension)
path.basename(p)
- Returns the path to the file name suffix
path.extname(p)
- Returns a string object path.
path.parse(path)
- Return path strings, and parse the object from the opposite
path.format(path)
url module
- What is the url?
- url is the world's Uniform Resource Locator, a kind of website resources concise expression, referred to as URLs
- url composition
- Complete
protocol: // username: password @ hostname domain name: port number / directory / file name extension parameter name = value & parameter parameter name = parameter value 2 2 # hash.? Common
protocol: // hostname domain name / directory / file name extension parameter name = value & parameter parameter name = parameter value 2 2 # hash.?2, the web crawler
Fourth, the network server
Five, express frame
Six, Mongodb database
Seven, mongoose operation database
- Complete