Home > Web Front-end > JS Tutorial > body text

Node.js Tools Module

黄舟
Release: 2017-01-17 16:10:58
Original
1282 people have browsed it

Learning points

Node.js os system operation function

Node.js Path processing file

Node.js Net network communication

Node. js Dns domain name resolution

Node.js Domain (domain)

Node.js tool module

Node.js os system operation function

Case: os .js

[code]var os = require('os');
// 返回操作系统的默认临时文件夹
console.log(os.tmpdir());
// 返回CPU的字节序
console.log(os.endianness());
// 返回 OS 的主机名
console.log(os.hostname());
// 返回 OS 名
console.log(os.type());
// 返回 OS 名称
console.log(os.platform());
// 返回 OS CPU 架构
console.log(os.arch());
// 返回 OS 的发行版本
console.log(os.release());
// 返回 OS 运行的时间,以秒为单位
console.log(os.uptime());
// 返回一个包含1、5、15分钟平均负载的数组
console.log(os.loadavg());
// 返回 OS 空闲内存量,单位是字节
console.log(os.freemem());
// 返回一个对象数组,包含所安装的每个 CPU内核的信息:
// 型号、速度、时间
console.log(os.cpus());
// 获得网络接口列表
console.log(os.networkInterfaces());
Copy after login

Node.js Tools Module

Node.js Path processing file

Case: path.js

[code]var path = require('path');
// 规范化路径,注意'..' 和 '.'
console.log('normalization : ' + path.normalize('/test/test1//2slashes/1slash/tab/..'));
// 连接路径
console.log('joint path : ' + path.join('/test', 'test1', '2slashes/1slash', 'tab', '..'));
// 转换为绝对路劲
console.log('resolve : ' + path.resolve('path.js'));
// 路径中文件的后缀名
console.log('ext name : ' + path.extname('path.js'));
Copy after login


Node.js Tools Module

Node.js Net Network Communication

Case: The browser cannot work

net.js

[code]var net = require('net');
var server = net.createServer(function (connection) {
    console.log('client connected');
    connection.on('end', function () {
        console.log('客户端关闭连接');
    });
    connection.write('Hello World!\r\n');
    connection.pipe(connection);
});
server.listen(8888, function () {
    console.log('server is listening');
});
Copy after login

Node.js Tools Module

Node.js Dns domain name resolution

Case: dns.js

[code]var dns = require('dns');
dns.lookup('www.lamport.me', function onLookup (err, address, family) {
    console.log('ip 地址:', address);
    dns.reverse(address, function (err, hostnames) {
        if (err) console.log(err.stack);
        console.log('反向解析' + address + ': ' + JSON.stringify(hostnames));
    })
})
Copy after login

Node.js Tools Module

Node.js Domain (domain)

Simplifies the exception handling of asynchronous code, and can catch and handle exceptions that cannot be caught by try catch

Case: domain.js

[code]var EventEmitter = require("events").EventEmitter;
var domain = require("domain");
var emitter1 = new EventEmitter();
// 创建域
var domain1 = domain.create();
domain1.on('error', function(err){
   console.log("domain1 处理这个错误 ("+err.message+")");
});
// 显式绑定
domain1.add(emitter1);
emitter1.on('error',function(err){
   console.log("监听器处理此错误 ("+err.message+")");
});
emitter1.emit('error',new Error('通过监听器来处理'));
emitter1.removeAllListeners('error');
emitter1.emit('error',new Error('通过 domain1 处理'));
var domain2 = domain.create();
domain2.on('error', function(err){
   console.log("domain2 处理这个错误 ("+err.message+")");
});
// 隐式绑定
domain2.run(function(){
   var emitter2 = new EventEmitter();
   emitter2.emit('error',new Error('通过 domain2 处理'));   
});
Copy after login

Node.js Tools Module

The above is the content of the Node.js tool module. For more related content, please pay attention to the PHP Chinese website (www.php.cn)!


Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!