Nodejs常用代码段

1、利用createReadStream方式计算大文件的md5签名

const fs = require('fs');
const crypto = require('crypto');
let path = '/target/file.data';
let start = new Date().getTime();
let md5sum = crypto.createHash('md5');
let stream = fs.createReadStream(path);
stream.on('data', function (chunk) {
    md5sum.update(chunk);
});
stream.on('end', function () {
    str = md5sum.digest('hex').toUpperCase();
    console.log('文件:' + path + ',MD5签名为:' + str + '.耗时:' + (new Date().getTime() - start) / 1000.00 + "秒");
});

 2、获取客房端IP地址

function getClientIp(req) {
    return req.headers['x-forwarded-for'] ||
        req.connection.remoteAddress ||
        req.socket.remoteAddress ||
        req.connection.socket.remoteAddress;
};

3、

posted @ 2017-07-03 15:29  yudis  阅读(352)  评论(0编辑  收藏  举报