This article has participated in the third “topic writing” track of the Denver Creators Training Camp. For details, check out: Digg Project | Creators Training Camp third is ongoing, “write” to make a personal impact.

preface

JavaScript is one of the most popular programming languages in the world. Today, it powers millions of websites and attracts hordes of developers and designers to build features for the web. JavaScript has become one of the best programming languages you have to master (I said it myself).

For the first 20 years, JavaScript was primarily used for client-side scripting. Because JavaScript can only be used within <script> tags, developers must use multiple languages and frameworks between front-end and back-end components. Then came Node.js, a runtime environment that includes everything you need to execute programs written in JavaScript.

Node.js is a single-threaded, open source, cross-platform runtime environment for building fast and scalable server-side and web applications. It runs on the V8 JavaScript runtime engine and uses an event-driven, non-blocking I/O architecture, making it efficient and suitable for real-time applications.

In this article, we’ll take a look at some useful but not common techniques for Node development

eventemitter

Eventemitter is a class provided by the Events module built into Node. It is the core of the Node event flow.

Many tripartite frameworks use this class, such as Electron. The parent (ipcMain) and the renderer (ipcRenderer) communicate using EventEmitter, which is an instance of EventEmitter. When used in the main process, it handles asynchronous and synchronous information sent from the renderer process (web page). Messages sent from the renderer process will be sent to this module.

The official sample

We’ll first register an EventEmitter object,

On (event, listener) : Registers a listener for the specified event, receiving a string event and a callback function.

Emit (Event, [arg1], [arg2]) : Executes each listener in the listener order

const EventEmitter = require('events'); class MyEmitter extends EventEmitter {} const myEmitter = new MyEmitter(); myEmitter.on('event', () => { console.log('an event occurred! '); }); myEmitter.emit('event');Copy the code

Other methods

AddListener (event, listener) adds a listener to the specified event and puts it at the end of the listener array. You can register multiple listeners for the same event.

Once (event, listener) Adds a listener for a specified event. However, this listener can be executed only once and is automatically destroyed after execution.

RemoveListener (event, Listener) destroys a listener for the specified event. Note that this listener must be identical to the listener in addListener. So the last step is to register the listener as a function.

var listener = function(stream) {
  console.log('listener');
};
myEmitter.on('event', listener);
myEmitter.removeListener('event', listener);
Copy the code

crypto

In some scenarios, such as when using Node for login and registration, it is impossible to transmit the password in plain text, so it needs to be encrypted. Previously, we might have used third-party packages for decryption and encryption. The Crypto module provides encryption functionality, including a complete package of functions for OpenSSL hashing, HMAC, encryption, decryption, signature, and authentication. Of course, not all encryption can be decrypted. For example, hMAC and Hash algorithms cannot be used for decryption. Symmetric encryption algorithms such as AES and DES can be used for encryption.

All supported encryption algorithms can be obtained via crypto.gethashes ().

The sample

md5

let crypto=require('crypto')
let result=crypto.createHash('md5').update('juejin').digest("hex");
console.log(result)
Copy the code

Results: 6 fc37ec86b0a6e1500c114639d701c45

encryption

/ / encryption
let key='123456789abcdefg'
let IV='abcdefg123456789'
let decipher = crypto.createCipheriv('aes-128-cbc', key,IV);
let result= decipher.update('l love juejin'.'binary'.'base64') + decipher.final('base64');
console.log(result)
/ / decryption
crypted = new Buffer(result, 'base64').toString('binary');
let decipher1 = crypto.createDecipheriv('aes-128-cbc', key, IV);
console.log(decipher1.update(crypted, 'binary'.'utf8') + decipher1.final('utf8'))
Copy the code

The Timer Timer

SetTimeout, setInternal, and setImmediate Mediate are some of the most common functions used in Node writing. Have you ever wondered where these functions come from?

In the browser node

You can see that these functions in Node are provided by the Timer module. The Timer module is the cornerstone module of node. The I/O operation in node uses Timer to record whether the request times out.

Vscode debug node

Click the sidebar debug button

Configure debugging parameters, click Run and Debug, and select nodeJS environment from the drop-down menu!

Basically the default is ok: the Program field specifies the entry.

Click start application to start testing.