electron 教程【6】与node.js相关的内容

electron基于Chrome进行页面显示,利用node与系统底层进行交互。所以,学习一些node相关知识,对写好代码很有必要。

首先讲一下require模块。

1 require模块

1.1模块引用

模块引用的示例代码如下:

var math = require('math');

在CommonJS规范中,存在require()方法,这个方法接收模块标识,以此引入一个模块的API到当前上下文。

1.2 模块定义

在模块中,上下文提供require()方法来引入外部模块。对应引入的功能,上下文提供了exports对象用于导出当前模块的方法或者变量,并且它是唯一导出的出口。在模块中,还存在一个module对象,它代表模块本身,而exports是module的属性。

在Node中,一个文件就是一个模块,将方法挂载在exports对象上作为属性即可定义导出的方法:

//math.js
exports.add = function() {
    var sum = 0, i = 0, args = arguments, l = args.length;
    while (i < l) {
        sum += args[i++];
    }
    return sum;
}

在另一个文件中,我们通过require()方法引入模块后,就能调用定义的属性或者方法了:

//test.js
var math = require('math');
exports.increment = function(val) {
    return math.add(val, 1);
}

1.3 模块标识

模块标识其实就是传递给require()方法的参数,它必须是符合小驼峰命名的字符串,或者以.、..开头的相对路径,或者是绝对路径。它可以没有文件名后缀.js。


然后是文件系统(file system)、网络和本地操作

2. fs模块-文件系统

Node.js 提供一组类似 UNIX(POSIX)标准的文件操作API

使用fs模块进行读取、写入、更名、删除、遍历目录、链接等文件操作。

fs模块中的方法均有异步和同步版本,例如读取文件内容的函数有异步的 fs.readFile() 和同步的 fs.readFileSync()。

异步的方法函数最后一个参数为回调函数,回调函数的第一个参数包含了错误信息(error)。

建议大家使用异步方法,比起同步,异步方法性能更高,速度更快,而且没有阻塞。

示例如下:

var path = require('path')
var filepath = path.join(__dirname, 'test.txt');
console.log(filepath);

var fs = require('fs');

fs.writeFile(filepath, "electron filesystem test", (err) => {
  if (!err) {
    console.log('write success!');
  } 
});

// read aysnc
fs.readFile(filepath, 'utf8', function (err, data) {
  if (err) {
    return console.log(err);
  } else {
    console.log('read success, content=' + data);
  }
});

// read sync
var data = fs.readFileSync(filepath);
console.log("sync read: " + data.toString());

fs更详细的使用方法见

  1. 官网api
  2. Node.js 文件系统

3.Net模块

Node.js Net 模块提供了一些用于底层的网络通信的小工具,包含了创建服务器/客户端的方法,我们可以通过以下方式引入该模块:

var net = require("net")

示例:
创建 server.js 文件,代码如下所示:

var net = require('net');
var server = net.createServer(function(connection) { 
   console.log('client connected');
   connection.on('end', function() {
      console.log('client close connect');
   });
   connection.write('Hello World!\r\n');
   connection.pipe(connection);
});
server.listen(8080, function() { 
  console.log('server is listening');
});

执行以上服务端代码:

$ node server.js
server is listening   # 服务已创建并监听 8080 端口

新开一个窗口,创建 client.js 文件,代码如下所示:

var net = require('net');
var client = net.connect({port: 8080}, function() {
   console.log('connected to server!');  
});
client.on('data', function(data) {
   console.log(data.toString());
   client.end();
});
client.on('end', function() { 
   console.log('disconnected with server!');
});

执行以上客户端的代码:

connected to server!
Hello World!
disconnected with server!

4. OS模块

Node.js OS 模块提供了一些基本的系统操作函数。

具体含义见:官网API

var os = require("os");
console.log('tmpdir : ' + os.tmpdir());
console.log('endianness : ' + os.endianness());
console.log('hostname : ' + os.hostname());
console.log('type : ' + os.type());
console.log('platform : ' + os.platform());
console.log('arch : ' + os.arch());
console.log('release : ' + os.release());
console.log('uptime : ' + os.uptime());
console.log('loadavg : ' + os.loadavg());
console.log('totalmem : ' + os.totalmem());
console.log('release : ' + os.release());
console.log('freemem : ' + os.freemem());

Windows平台的输出示例:

D:\workspace\practiceNodeJs\src>node os.js
tmpdir : C:\Users\gavinxu\AppData\Local\Temp
endianness : LE
hostname : gavin
type : Windows_NT
platform : win32
arch : x64
release : 10.0.14393
uptime : 52568.6383051
loadavg : 0,0,0
totalmem : 8497946624
release : 10.0.14393
freemem : 4029931520
参考原文链接: 点击打开链接

猜你喜欢

转载自blog.csdn.net/jigetage/article/details/81004699
今日推荐