Node.js基础实战

一.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);

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值