In the last article, we briefly introduced Node.js. Learned that it was based on JavaScript, asynchronous by nature, and had a large number of third-party libraries. This article will provide a more in-depth introduction to Node.js. The main contents include:

  • The installation of the Node
  • How to use third-party module ecology
  • Installation of third-party modules
  • Some simple usage examples
  • Some suggestions and tips for development

Until then, I assume that you already know the basics of JavaScript and are familiar with some basic command-line operations. Also, don’t assume you have a complete grasp of Node from this chapter. But if you want to, read about Node.js in action.

Install the Node

One of the things about the JavaScript world is that it’s very selective, and Node installation is no exception.

You can find various versions of the source code and installation package files on the official download page. You are advised to use the installation package corresponding to your own operating system. Of course, you can also use apt-get, Homebrew and other package managers to install, if your system has. For details, see the installation guide of the official package management tool.

If you are using a Mac or Linux, I highly recommend using NVM for installation. The corresponding program on Windows is NVMW. These version management tools allow you to switch between different versions. For example, you can try out features in a new version while keeping a stable version in the system. In addition, NVM requires no system administration rights and is very easy to uninstall. The installation process requires only one command from the terminal.

Now, install Node on your system.

Run your first Node script

Once the installation is complete, start by writing “Hello World” to check some things out. Add the following code to the new helloworld.js:

console.log("Hello, World!");Copy the code

The main thing in the code is to use console.log to print the string “Hello,world!” , believe that front-end programmers will not feel strange. Let’s run the code using Node Helloworld.js. If all is well, the following output appears:

02 _01

Use of modules

In most programming languages, we split up the code and then import these files when we use them. For example, include in C and C++, Import in Python, require in Ruby and PHP. Other languages, such as C#, make cross-file references at compile time.

For a long time, JavaScript officially did not support the module mechanism. So someone in the community wrote RequireJS to solve the dependency import problem. However, most of the time file imports are done with \ labels. Node solves this problem perfectly by implementing a standard module called CommonJS.

The module system has three main contents: the introduction of built-in module, the introduction of third-party module, the introduction of personal private module. Each of these will be introduced in the following sections.

Introducing built-in modules

Node already has a number of built-in utility modules, such as the file system module FS and utility function module util.

The most common task in a Web application written by Node is URL parsing. The browser requests the corresponding resource on the server through a specific URL. For example, access to the home page, access to network requests about the page. These urls are in the form of strings that we need to parse to get more information. Here we show how to introduce built-in modules by parsing urls.

There aren’t many methods exposed in the built-in URL module, but there is a parse function that is very useful. It can extract useful information such as domain names and paths from URL strings.

Here we use require to implement module Import, which is the same as Include and Import mentioned earlier. By taking the module name as an argument, the command successfully returns the corresponding module. In most cases, the object returned is an Object object, but it can also be a string, number, or function. Here is sample code to introduce the change module:

var url = require("url");   
var parsedURL = url.parse("http://www.example.com/profile?name=barry");  

console.log(parsedURL.protocol);  // "http:"
console.log(parsedURL.host);       // "www.example.com"
console.log(parsedURL.query);     // "name=barryCopy the code

In the above code, a module object is returned via require(” URL “), and the object’s methods can then be called just like any other object. Save this code in url-test.js and run the node url-test.js command to see the protocol name, domain name, and query criteria.

In addition, most of the time we import modules with a variable of the same name to accept the returned module object. For example, the url is used above to introduce the return value of require(“url”). Of course, you don’t have to follow the above rules. You can also do this if you want:

var theURLModule = require("url");   
var parsedURL = theURLModule.parse("http://www.example.com/profile?name=barry");Copy the code

Keeping variable and module names consistent is a loose convention for readability in a uniform style, not a mandatory specification.

Import third-party modules using NPM and package.json

Node’s built-in modules are far from sufficient for daily development, so introducing third-party modules is a skill that must be mastered.

First, we need to understand the package.json file. All Node projects are stored in a separate folder, and if a project uses third-party modules, there must be a file named package.json. The contents of package.json are very simple, usually defining the project name, version number, author, and external dependencies of the project.

In the newly created Node project folder, copy the following contents into package.json.

{
  "name": "my-fun-project"."author": "Evan Hahn"."private": true."version": "0.2.0"."dependencies": {}}Copy the code

There is actually another program installed during the Node installation: NPM. NPM is often referred to as the Node package manager, and this is one of its best features. Let’s say you now need to import Mustache, a little standard template system, into your app. It can convert template strings into real strings, as shown in the code:

// Returns "Hello, Nicholas Cage!"
Mustache.render("Hello, {{first}} {{last}}!", {
  first: "Nicholas".last: "Cage"
});

// Returns "Hello, Sheryl Sandberg!"
Mustache.render("Hello, {{first}} {{last}}!", {
  first: "Sheryl".last: "Sandberg"
});Copy the code

Now, suppose you want to write a simple Node application to welcome Nicolas Cage with the Mustache module.

First, run NPM Install Mustache –save in the root of the project folder. The command creates a new node_modules folder and saves Mustache to that folder. The –save argument will add the module to the pakage.json file. At this point the pakage.json folder looks something like this, with Mustache using the latest version.

{
  "name": "my-fun-project"."author": "Evan Hahn"."private": true."version": "0.2.0"."dependencies": {
    "mustache": "^ 2.0.0." "  #A
  }
}Copy the code

If you don’t use the –save option, pakage.json will remain unchanged, although the node_modules folder will be created and the Mustache module will be saved to a subdirectory of the same name. The reason for storing these dependencies in package.json is so that other developers can use NPM Install to install all dependencies when they get the project. Another reason is that Node projects typically ignore the node_modules folder for code management and keep package.json.

After the installation is complete, the next step is to use:

var Mustache = require("mustache");  
var result = Mustache.render("Hi, {{first}} {{last}}!", {
  first: "Nicolas".last: "Cage"
});
console.log(result);Copy the code

Save the code to mustache test.js and execute the node mustache test.js command. Then you will see Hi Nicolas Cage! .

It’s as simple as that. Once these dependencies are installed, you can call them as if you were using a built-in module. Node_modules does all the work that modules do directly to Node, so you don’t have to worry about it.

Of course you can add project dependencies manually, and you can specify the version of the dependency.

NPM init can do other things besides installing dependencies. For example, generate package.json automatically instead of manually editing it. In a new project folder, you can configure the project name, author, version, and so on by using NPM init. NPM then generates the corresponding package.json file. This automated process saves developers time.

Implementing private modules

Now that you’ve seen how to use modules developed by others, you’ll learn how to develop a proprietary module. Suppose you now want to return a random integer between 0 and 100. Without introducing additional modules, the code looks like this:

var MAX = 100;
function randomInteger()  {
    return Math.floor( (Math.random() * MAX) );
}Copy the code

This is probably similar to what you would code in a browser environment, nothing special. But in Node, we also need to expose a variable for external use. This allows other programs to get the variable when they introduce it through require. In this case, we expose the function randomInteger and save the code to the random-integer.js file.

var MAX = 100;
function randomInteger()  {
    return Math.floor( (Math.random() * MAX) );
}

module.exports = randomInteger;Copy the code

The last line of code may feel a bit foreign to Node beginners. Only one variable can be exposed per module and must be set through module.exports. In this example, only one function variable is exposed, so MAX is a module private variable that cannot be accessed by other files.

Module.exports can expose any variable, which in this case is a function but would normally be an object. Of course, you can expose strings or arrays.

Let’s use this new module. In the same directory as random-integer.js, create a new print-three-random-integers.

var randomInt = require("./random-integer"); #A console.log(randomInt()); // 12 console.log(randomInt()); // 77 console.log(randomInt()); / / 8Copy the code

Except that you need to specify relative paths through point syntax, the rest is pretty much the same. Using the node print-three-random-integers. Js command, we can check the program’s performance. As expected, three random numbers between 0 and 100 will be printed.

If you try to run Node random-integer.js, nothing happens. Although we have exposed the function in the module, the modification function will not be executed and will not print any output.

Note that only the use of private modules in projects is covered here. If you want to publish your modules for others to use, check out my personal site.

That’s a simple introduction to the Node module system.

Node: Asynchronous world

In Chapter 1, I briefly introduced the asynchronous nature of Node using the “baking muffins” example. The point is, you can’t do two things at once even if they’re happening at the same time. I get to work out while baking, but the oven is just an external thing.

02 _02

Node asynchrony works in a similar way, for example, if you request an image of a kitten from the Node server through your browser. Because the image resource is so large, you can do other things while reading and writing to disk. At this point, the disk acts as an external resource and we can process the second request directly without having to suspend and wait for the time-consuming operation to finish.

There are two main external resources in Express:

  1. The file system is involved. For example, reading and writing disk files.
  2. Involves network processing. For example, accept a request and send a response.

In Node code, these asynchrony are handled through callbacks. It works the same way as sending AJAX requests on Web pages. You send the request with a callback function, and your callback will be executed when the request processing is complete.

For example, now you are reading the file myfile.txt on your hard disk. When finished reading, you want to be able to print out the number of occurrences of the letter X as follows:

var fs = require("fs");  

var options = { encoding: "utf-8" };                      
fs.readFile("myfile.txt", options, function(err, data) {  
  if (err) {                                
    console.error("Error reading file!");   
    return;                                 
  }                                        

  console.log(data.match(/x/gi).length + " letter X's");  
});Copy the code

Let’s explain the code step by step:

First, we import the file system module that comes with Node. This module mainly deals with file related content, most of which are file reading and writing functions. One of the readFile methods used in this example.

Next, we need to set the parameters in the fs.readFile method, the first is the file name and the second is the callback function. And the callback function is executed after reading.

Most Node callback functions set the error message as the first argument. In normal cases, this parameter is equal to null. If an error occurs, this parameter saves error information. Although sometimes these error messages do not cause the program to terminate, in most cases we need to respond to the error by, for example, throwing an exception and jumping out of the callback function. This is also the most common callback practice in Node.

Finally, when all is well, we use a regular expression to match the letter X and print its number.

So let’s do a test. Here, we add a paragraph at the end of the code above, so what happens?

var fs = require("fs");  

var options = { encoding: "utf-8" };                      
fs.readFile("myfile.txt", options, function(err, data) {  
  if (err) {                                
    console.error("Error reading file!");   
    return;                                 
  }                                        

  console.log(data.match(/x/gi).length + " letter X's");  
});

console.log("Hello World!");Copy the code

Asynchronous files are read asynchronously, so the first print is “Hello world! “, followed by the print operation in the asynchronous function.

This is where the asynchronous pattern is powerful. While an external device is handling a time-consuming operation, you can continue to run other code. In Web applications this means that more requests can be processed in the same amount of time.

Note: If you want to learn more about asynchronous JavaScript, you can check out this video on YouTube. The instructions in this video apply to both Node and browser environments.

Building Web services with Node: HTTP modules

Only by understanding these concepts will you be able to better understand Node’s built-in HTTP modules. This module is one of the most important modules for the Express framework. It is in this module that Node and Express are able to build Web services.

Node’s HTTP module has many features (such as sending network requests to other servers), but we’ll use one of them called http.createserver. The method processes and responds to each network request through its callback function. In the following code we set all responses to “Hello World” (which can be saved to myServer.js).

var http = require("http");           

function requestHandler(request, response) {             
  console.log("In comes a request to: " + request.url);  
  response.end("Hello, world!");                         
}                                                        

var server = http.createServer(requestHandler);  
server.listen(3000);Copy the code

The code above is made up of four parts.

First, we introduce the HTTP module and save it to the variable HTTP. This is consistent with the previous actions of the URL module.

Next, a requestHandler function, requestHandler, is defined. Almost all of the code in the tutorial is either a request handler or a call handler. This function takes two arguments, request representing the request object and response representing the response object. The request contains the URL path and user-agent information. By calling the Response object method Node wraps up the response information and sends it to the requester.

The rest of the code specifies the handler function that the built-in HTTP service executes on the request and the port number that the service listens on.

For HTTPS, we can use the built-in HTTPS module. The process is the same except that you need to configure the SSL certificate. If you know HTTPS, you can switch from HTTP to HTTPS in two minutes. Even if you don’t know, don’t worry too much.

If you save the code to myServer.js and execute Node myServer.js to pull up the service. Now, if you go to http://localhost:3000 in your browser, you should see:

02 _03

You may also have noticed that the terminal console prints something whenever you make a request. When you try to access different urls, the console prints different messages but gets a “Hello, world!” response. . The console prints information like:

02 _04

Note that the URL information printed above does not include localhost:3000. It may not seem intuitive, but the reverse is also true. After all, using relative paths, we can deploy Node applications on any computer without modification.

The code for URL parsing is roughly as follows:

function requestHandler(req, res) {
    if (req.url === "/") {
        res.end("Welcome to the homepage!");
    } else if (req.url === "/about") {
        res.end("Welcome to the about page!");
    } else {
        res.end("Error! File not found."); }}Copy the code

All request urls can be processed in this function. This is really easy for simple applications, but as the application grows larger the function becomes bloated and difficult to maintain. This is where the Express framework comes in.

## Summarize the main contents of this paper:

  • The installation of the Node
  • Use of modular systems
  • An introduction to package.json files
  • Install the third module dependency through package.json
  • Asynchronous programming concepts in Node.
  • Creation of a simple HTTP service application.

The original address