Node.js modular

Time:2020-10-18

In practical work, when writing a little larger program, the code is usually modularized. In nodejs, the code is generally divided into different JS files reasonably. Each file is a module, and the file path is the module name.

When writing each module, there arerequireexportsmoduleThree predefined variables are available.

Built in module

Node.js There is a set of built-in modules that can be used without further installation. For example, assert, buffer, child_ Process, cluster, etc. Of course, in addition to the built-in modules, we can also install third-party modules through the NPM command.

Use of modules

Before using a module, we need to use the require() instruction to load the module, as follows:

Var HTTP = require ('module name ');
Example:

For example, use an application to access the HTTP module and create a server:

var http = require('http');
http.createServer(function (req, res) {
    res.writeHead(200, {'Content-Type': 'text/html;charset=utf-8'});
    res.end (chivalrous class Island);
  }).listen(8888);

After starting the file, access it in the browser http://127.0.0.1 8888 /, the page will display the words “chivalrous class island” (I will not take a screenshot here, you can try it yourself).

How to create a module

Except that it can be used Node.js Or use a third-party module. We can also create our own modules and include them in the application. This is actually very simple, because we know that files and modules are one-to-one, one Node.js A file is a module.

Example:

Create a module that returns date and time objects and save as mymodule.js Medium. amongmymoduleIt is the name I defined for the file, which is also the module name of the module. As follows:

exports.myDateTime = function () {
  return Date();
};

In the above code,Date()Is the function used to get the current date and time,exportsThe function of the keyword is to make the properties and methods outside the module file available. This is understandable. For example, there are two JS files one.js And two.js If we want to two.js Visit China one.js It’s not possible to use the method in. So what should we do? You can use itexportsExpose the module interface so that we can access the module in another file.

In another one Node.js File using themymoduleModule, the first thing to do is to introduce thismymoduleModule, and then use it as follows:

var http = require('http');
var dt = require('./mymodule');

http.createServer(function (req, res) {
  res.writeHead(200, {'Content-Type': 'text/html; charset=utf-8'});
  res.write (current date and time is:+ dt.myDateTime ());
  res.end();
}).listen(8888);

Save the code to test_ module.js File, and then run the file. The command to run the file is as follows:

node test_module.js

Access in browser http://127.0.0.1 : 8888 /, the page will display the following contents:

Current date and time: Thu APR 02 2020 16:50:19 GMT + 0800 (China standard time)

As shown in the figure:

Node.js modular

Note that the./To locate the module, indicating that the module and Node.js The files are in the same folder. Be sure to pay attention to the path of the module when introducing the module. Otherwise, an error will be reported if the module cannot be found.

Random code problem

If we use Chinese in the code, we need to add the following sentence:

res.writeHead(200, {'Content-Type': 'text/html; charset=utf-8'});

charset=utf-8Used to set the encoding, indicating that the character set of the current document is UTF-8. UTF-8 is a variable length character encoding for Unicode, which can be used to represent any character in the Unicode standard.

More can be found: https://www.9xkd.com/3716132715.html

Recommended Today

It is too difficult to manage online service? One stop service management platform is coming!

Course summary Nowadays, microservice has become the dominant mode of building modern cloud applications. It decomposes individual components into independent services around specific business functions. But then there are other problems: more and more systems have been disassembled into many cell like microservices. How to manage the microservices has become a headache for many engineers. […]