📜  电子-本机节点库

📅  最后修改于: 2020-10-25 10:55:01             🧑  作者: Mango


在上一章中,我们使用了一个节点模块fs。现在,我们将介绍可与Electron一起使用的其他一些节点模块。

操作系统模块

使用OS模块,我们可以获得有关运行应用程序的系统的很多信息。以下是在创建应用程序时提供帮助的一些方法。这些方法可帮助我们根据正在运行的操作系统自定义应用程序。

Sr.No Function & Description
1

os.userInfo([options])

The os.userInfo() method returns information about the currently effective user. This information can be used to personalize the application for the user even without explicitly asking for information.

2

os.platform()

The os.platform() method returns a string identifying the operating system platform. This can be used to customize the app according to the user OS.

3

os.homedir()

The os.homedir() method returns the home directory of the current user as a string. Generally, configs of all users reside in the home directory of the user. So this can be used for the same purpose for our app.

4

os.arch()

The os.arch() method returns a string identifying the operating system CPU architecture. This can be used when running on exotic architectures to adapt your application for that system.

5

os.EOL

A string constant defining the operating system-specific end-ofline marker. This should be used whenever ending lines in files on the host OS.

使用相同的main.js文件和以下HTML文件,我们可以在屏幕上打印这些属性-

OS Module
   
   
   
      
   

现在使用以下命令运行应用程序-

$ electron ./main.js

上面的命令将生成以下输出-

User Info: {"uid":1000,"gid":1000,"username":"ayushgp","homedir":"/home/ayushgp",
   "shell":"/usr/bin/zsh"}
Platform: linux
User home directory: /home/ayushgp
OS Architecture: x64

网络模块

net模块用于应用程序中与网络相关的工作。我们可以使用此模块创建服务器和套接字连接。通常,建议使用npm的包装器模块,而不是将net模块用于与网络相关的任务。

下表列出了模块中最有用的方法-

Sr.No Function & Description
1

net.createServer([options][, connectionListener])

Creates a new TCP server. The connectionListener argument is automatically set as a listener for the ‘connection’ event.

2

net.createConnection(options[, connectionListener])

A factory method, which returns a new ‘net.Socket’ and connects to the supplied address and port.

3

net.Server.listen(port[, host][, backlog][, callback])

Begin accepting connections on the specified port and host. If the host is omitted, the server will accept connections directed to any IPv4 address.

4

net.Server.close([callback])

Finally closed when all connections are ended and the server emits a ‘close’ event.

5

net.Socket.connect(port[, host][, connectListener])

Opens the connection for a given socket. If port and host are given, then the socket will be opened as a TCP socket.

网络模块也带有其他一些方法。要获得更全面的列表,请参阅

现在,让我们创建一个电子应用程序,该应用程序使用net模块创建与服务器的连接。我们将需要创建一个新文件server.js-

var net = require('net');
var server = net.createServer(function(connection) { 
   console.log('Client Connected');
   
   connection.on('end', function() {
      console.log('client disconnected');
   });
   
   connection.write('Hello World!\r\n');
   connection.pipe(connection);
});

server.listen(8080, function() { 
   console.log('Server running on http://localhost:8080');
});

使用相同的main.js文件,将HTML文件替换为以下内容-

net Module
   
   
   
      
   

使用以下命令运行服务器-

$ node server.js

使用以下命令运行应用程序-

$ electron ./main.js

上面的命令将生成以下输出-

网络模块

观察到我们会自动连接到服务器,并且也会自动断开连接。

我们还有其他一些节点模块,可以使用Electron在前端直接使用。这些模块的用法取决于您在其中使用的方案。