一.http模块
1.搭建http服务
const http = require('http');
let server = http.createServer((req,res) => {
console.log(req.method + ":" + req.url);
res.writeHead(200,{'Content-Type':'text/html; charset=UTF-8'});
let body = `<h1>欢迎来到Node.js的世界<h1><h4>method:${req.method},url:${req.url}</h4>`;
res.end(body);
});
server.listen(3000);
2.搭建文件服务器
const http = require('http');
const fs = require('fs');
const url = require('url');
const path = require('path');
let root = __dirname;
let server = http.createServer((req,res)=>{
let pathname = url.parse(req.url).pathname;
let filepath = path.join(root,pathname);
fs.stat(filepath,(err,data)=>{
if(!err && data.isFile()){
console.log('200 ' + req.url);
res.writeHead(200);
fs.createReadStream(filepath).pipe(res);
}else{
console.log('404 ' + req.url);
res.writeHead(404);
res.end('404 not Found');
}
})
});
server.listen(3000);
二.fs模块
1.读文件
const fs = require('fs');
fs.readFile('sample.txt','utf-8',(err,data)=>{
if(err){
console.log('读取失败');
}else{
console.log(data);
}
});
2.写文件
const fs = require('fs');
let data = 'hello,Node.js';
fs.writeFile('output.txt',data,(err,data)=>{
if(err){
console.log(err);
}else{
console.log('ok');
}
});
3.读取文件信息
const fs = require('fs');
fs.stat('sample.txt',(err,stats)=>{
if(err){
console.log(err);
}else{
console.log("isFile:" + stats.isFile());
console.log("isDirectory:" + stats.isDirectory());
if(stats.isFile()){
console.log("size:" + stats.size);
console.log("birth time:" + stats.birthtime);
console.log("modified time:" + stats.mtime);
}
}
});
4.以文件流的形式读取文件内容
const fs = require('fs');
let rs = fs.createReadStream("sample.txt",'utf-8');
rs.on('data',(chunk)=>{
console.log('data:' + chunk);
});
rs.on('end',()=>{
console.log('end');
});
rs.on('error',(err)=>{
console.log('error:' + err);
});
5.以流的形式写入文件
const fs = require('fs');
let ws1 = fs.createWriteStream('output1.txt','utf-8');
ws1.write('使用stream写入文本数据...\n');
ws1.write('end');
ws1.end();
let ws2 = fs.createWriteStream('output2.txt','utf-8');
ws2.write(new Buffer('使用stream写入文本数据...\n'));
ws2.write(new Buffer('end'));
ws2.end();
6.以管道形式写入文件
const fs = require('fs');
let rs = fs.createReadStream('sample.txt');
let ws = fs.createWriteStream('copied.txt');
rs.pipe(ws);
三.crypto模块
1.md5加密或sha1加密
const crypto = require('crypto');
// const hash = crypto.createHash('md5');
const hash = crypto.createHash('sha1');
hash.update('Hello,world!');
hash.update('Hello,nodejs!');
console.log(hash.digest('hex'));
2.Hmac加密
const crypto = require('crypto');
const hmac = crypto.createHmac('sha256','secret-key');
hmac.update('hello,world');
console.log(hmac.digest('hex'));
3.AES对称加密
const crypto = require('crypto');
function aesEncrypt(data, key) {
const cipher = crypto.createCipher('aes192', key);
let crypted = cipher.update(data, 'utf8', 'hex');
crypted += cipher.final('hex');
return crypted;
}
function aesDecrypt(encrypted, key) {
const decipher = crypto.createDecipher('aes192', key);
let decrypted = decipher.update(encrypted, 'hex', 'utf8');
decrypted += decipher.final('utf8');
return decrypted;
}
let data = 'Hello, this is a secret message!';
let key = 'Password!';
let encrypted = aesEncrypt(data, key);
let decrypted = aesDecrypt(encrypted, key);
console.log('Plain text: ' + data);
console.log('Encrypted text: ' + encrypted);
console.log('Decrypted text: ' + decrypted);