Nodejs simple getting started tutorial (1): module mechanism, nodejs simple

Source: Internet
Author: User
Tags install redis

Nodejs simple getting started tutorial (1): module mechanism, nodejs simple

The JavaScript specification (ECMAScript) does not define a complete set of standard libraries that can be applied to most programs. CommonJS provides a set of JavaScript standard library specifications. Node implements the CommonJS specification.

Module Basics

In Node, modules and files correspond one by one. We define a module:
Copy codeThe Code is as follows:
// Circle. js
Var PI = Math. PI;
 
// Export the function area
Exports. area = function (r ){
Return PI * r;
}
 
// Export the function circumference
Exports. circumference = function (r ){
Return 2 * PI * r;
};

Add the function to be exported to the exports object. The local variables of the module cannot be accessed externally (for example, the PI variable in the previous example ). Call require to load the module circle. js:
Copy codeThe Code is as follows:
Var circle = require ('./circle. js ');
Console. log ('the area of a circle of radius 4 is'
+ Circle. area (4 ));

It is mentioned that a module object exists in the module, indicating the module itself, and exports indicates the module attribute.

Module Loading

Node caches loaded modules to avoid overhead of re-loading:
Copy codeThe Code is as follows:
// Test. js
Console. log ("I'm here ");

Multiple Loading modules test. js

Copy codeThe Code is as follows:
// Output "I'm here" only once"
Require ('./test ');
Require ('./test ');

When the file to be loaded has no suffix, Node will try to add the suffix and load it:

1.. js (JavaScript source file)
2. node (C/C ++ extension module)
3. json (JSON file)

There are several main modules:

1. core modules. The core modules have been compiled into Node. We can find these core modules in the lib directory of the source code. Common core modules: net, http, and fs

2. File module. The file module is loaded through a relative or absolute path, for example, the circle. js shown above

3. Custom module. The custom module is located in the node_modules directory. The modules we install through npm are placed in the node_modules directory.

The core module is always loaded first. If there is a custom module http, the core module http will still be loaded while loading, not the custom module http. When loading a custom module, first find the node_modules directory under the current directory, then find the node_modules directory under the parent directory, and so on until the root directory.

When a module loaded by require is not a file but a directory, such a directory is called a package ). The package contains a file named package. json (package Description file), for example:
Copy codeThe Code is as follows:
{"Name": "some-library ",
"Main": "./lib/some-library.js "}

Main indicates the module to be loaded. If the main module is not specified in the package. json or package. json, Node will try to load index. js, index. node, and index. json.

When loading the JavaScript module, the loaded module is wrapped in a function:
Copy codeThe Code is as follows:
Function (module, exports, _ filename, _ dirname ,...){
JavaScript module
}

The module, exports, _ filename, and _ dirname accessed by each JavaScript module are actually transmitted through function parameters. Because of this package, the module's local variables cannot be accessed externally. However, sometimes there are hard-to-understand problems, such:

Test1.js

Copy codeThe Code is as follows:
Exports = {
Name: 'name5566 ',
}

Test2.js

Copy codeThe Code is as follows:
Module. exports = {
Name: 'name5566 ',
}

Load these two modules:

Copy codeThe Code is as follows:
Var test1 = require ('./test1.js ');
Console. log (test1.name); // undefined
Var test2 = require ('./test2.js ');
Console. log (test2.name); // Name5566

The exports parameter is passed to the module through exports. x can naturally add properties (or methods) to the exports object, but assigning values directly to exports (for example, exports = x) only changes the values of the form parameter rather than the real parameter. Therefore:

1. When adding properties for exports, use exports
2. When assigning values to exports, use module. exports

Package

According to CommonJS specifications, a complete package should include:

1. package. json package description file
2. bin binary file directory
3. lib JavaScript code directory
4. doc document directory
5. test code directory

NPM is a Node package management tool. Common usage:

View the command documentation:

Copy codeThe Code is as follows:
Npm help install

View the install command documentation.

Install a package:
Copy codeThe Code is as follows:
Npm install redis

Install the redis package. The install command installs the package in the node_modules directory under the current directory.

Remove a package:
Copy codeThe Code is as follows:
Npm remove redis

Remove the redis package. The remove command removes the packages in the current directory.


For a person with a js base, is nodejs hard or php hard?

Your js is just the foundation. nodejs is the backend framework, and there is a difference between the two.
If you are interested in js, you can look at nodejs. Otherwise, you will learn php and learn one language well. If you change the language, you will be able to get started quickly. It is important to be proficient in one language.

Develop a website using php or nodejs?

I don't think node. js is not suitable for new users. After all, you have to learn PHP from scratch and have a learning process for Web programming. Therefore, the two are almost equivalent in terms of getting started. Also, you need to know about MVC and database connections.
You can compare the advantages and disadvantages of getting started.
Node. js
Advantage: js syntax is not difficult for JavaScript users to understand the code.

It simplifies the server configuration process and comes with a web server. Unlike php, apache is not required for installation and configuration.
Disadvantages: less learning materials than PHP
PHP
Advantages: many learning materials, many frameworks, and many tool kits
Disadvantage: familiar with syntax
I think this should be your first website. The importance of language is not that great. The advantages and disadvantages may not be reflected in your learning process, and they are also external factors. Of course, you can also think about it like this. Now we are focusing on PHP, and most of them are used by lightweight companies. Node has been promoted by many large companies, and more opportunities are available in the early stage.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.