Nodejs 笔记
event Module
const EventEmitter
= require('events');
const eventEmitter
= new EventEmitter();
eventEmitter
.on('tutorial',(num1
,num2
)=>{
console
.log('tutorial event has occurred');
console
.log(num1
+ num2
);
})
eventEmitter
.emit('tutorial',1,2);
class Person extends EventEmitter {
constructor(name
){
super();
this._name
= name
;
}
get name(){
return this._name
;
}
}
let pedro
= new Person('Pedro');
pedro
.on('name',()=>{
console
.log('My name is ' + pedro
.name
)
})
pedro
.emit('name');
readline Module
const readline
= require('readline');
const rl
= readline
.createInterface({input
: process
.stdin
,output
: process
.stdout
});
let num1
= Math
.floor(Math
.random()*10 + 1);
let num2
= Math
.floor(Math
.random()*10 + 1);
let answer
= num1
+ num2
;
rl
.question(`What is ${ num1 } + ${ num2 }? \n`,(userInput
)=>{
if(userInput
.trim() == answer
){
rl
.close();
}else{
rl
.setPrompt('False Result Please try again.\n');
rl
.prompt();
rl
.on('line',(userInput
)=>{
if(userInput
.trim() == answer
){
rl
.close();
}else{
rl
.setPrompt(`Your answer ${userInput} is incorrect,please try again.\n`);
rl
.prompt();
}
})
}
});
rl
.on('close',()=>{
console
.log('True');
});
FileSystem Module
const fs
= require('fs');
fs
.writeFile('example.txt','this is a example',(err
)=>{
if(err
){
console
.log('err');
}else{
console
.log('File successfully created');
fs
.readFile('example.txt','utf8',(err
,file
)=>{
if(err
){
console
.log(err
);
}else{
console
.log(file
);
}
})
}
})
fs
.rename('example.txt','example2.txt',(err
)=>{
if(err
){
console
.log(err
);
}else{
console
.log('File successfully renamed');
}
})
fs
.appendFile('example2.txt',', Some data been appended',(err
)=>{
if(err
){
console
.log(err
);
}else{
console
.log('File successfully appended.');
}
})
fs
.unlink('example2.txt',(err
)=>{
if(err
){
console
.log(err
);
}else{
console
.log('File successfully deleted.');
}
})
fs
.mkdir('tutorial',(err
)=>{
if(err
){
console
.log(err
);
}else{
console
.log('folder successfully created');
}
})
fs
.rmdir('tutorial',(err
)=>{
if(err
){
console
.log(err
);
}else{
console
.log('folder successfully deleted');
}
})
fs
.mkdir('tutorial',(err
)=>{
if(err
){
console
.log(err
);
}else{
console
.log('folder successfully created');
fs
.writeFile('./tutorial/example.txt','12345678',(err
)=>{
if(err
){
console
.log(err
);
}else{
console
.log('file successfully created')
}
})
}
})
fs
.readdir('tutorial',(err
,files
)=>{
if(err
){
console
.log(err
);
}else{
console
.log(files
);
for(let file
of files
){
fs
.unlink('./tutorial/'+file
,(err
)=>{
if(err
){
console
.log(err
);
}else{
console
.log(`successfully deleted the file ${file}`);
}
})
}
}
})
流式读取文件
const fs
= require('fs');
const readStream
= fs
.createReadStream('./example.txt','utf8');
const writeStream
= fs
.createWriteStream('./example2.txt');
readStream
.on('data',(chunk
)=>{
console
.log(chunk
);
writeStream
.write(chunk
);
console
.log('文件写入成功!')
});
注:以上内容和以下内容等价
const fs
= require('fs');
const readStream
= fs
.createReadStream('./example.txt','utf8');
const writeStream
= fs
.createWriteStream('./example2.txt');
readStream
.pipe(writeStream
);
压缩和解压
const fs
= require('fs');
const zlib
= require('zlib');
const readStream
= fs
.createReadStream('./example.txt','utf8');
const writeStream
= fs
.createWriteStream('./example2.gz');
const gzip
= zlib
.createGzip();
readStream
.pipe(gzip
).pipe(writeStream
);
const fs
= require('fs');
const zlib
= require('zlib');
const readStream
= fs
.createReadStream('./example2.gz');
const writeStream
= fs
.createWriteStream('./uncompressed.txt');
const gunzip
= zlib
.createGunzip();
readStream
.pipe(gunzip
).pipe(writeStream
);
http Module
const http
= require('http');
const server
= http
.createServer((req
,res
)=>{
if(req
.url
=== '/'){
res
.write('Hello BLU from node.js');
res
.end();
}else{
res
.write('use some other domain');
res
.end();
}
});
server
.listen('3000');
静态资源托管
const http
= require('http');
const fs
= require('fs');
http
.createServer((req
,res
)=>{
const readStream
= fs
.createReadStream('./static/baidu.png');
res
.writeHead(200,{'Content-type':'image/png'});
readStream
.pipe(res
);
}).listen(3000);
const http
= require('http');
const fs
= require('fs');
http
.createServer((req
,res
)=>{
const readStream
= fs
.createReadStream('./static/index.html');
res
.writeHead(200,{'Content-type':'text/html'});
readStream
.pipe(res
);
}).listen(3000);
const http
= require('http');
const fs
= require('fs');
http
.createServer((req
,res
)=>{
const readStream
= fs
.createReadStream('./static/example.json');
res
.writeHead(200,{'Content-type':'application/json'});
readStream
.pipe(res
);
}).listen(3000);
npm init
npm init 可用来初始化生成一个新的 package.json 文件:
{
"name": "src",
"version": "1.0.0",
"description": "BLU",
"main": "app.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "BLU",
"license": "ISC",
"dependencies": {}
}
npm install
npm install 命令用来安装模块 示例:
npm install lodash
转载请注明原文地址:https://ipadbbs.8miu.com/read-2570.html