node. JS learns 01:module System and some common node Module

Source: Internet
Author: User

node. JS learns to follow the flow of this book.

At the end of the 7th chapter and the end of the 10th chapter, respectively, a small project to practice practiced hand. Node. JS's Introductory Learning Plan is like this.

Directory:

qq:1045642972 Welcome to the book and discuss node. js.

node. js Demo

The node. JS website provides a basic demo Code:

var http = require (' http '); http.createserver(function  (req, res) {    Res.writehead ( {' Content-type ': ' Text/plain '});    Res.end (' Hello World '). Listen (1337, ' 127.0.0.1 '); Console.log (' Server running at http://127.0.0.1:1337/');

After running, enter the URL in the browser, the result:

Require (' http '); is to get the exported object from the HTTP node. JS provided by this module for subsequent operations to create a listening port.

Module

4 Essentials of the Module system:

    • Each JS file maps a module.
    • In each JS file, it is possible to directly manipulate the module by using the module variable.
    • Export module with the help of module.exports this variable
    • The import module is based on the Require global function.

Directly on the code to illustrate

File structure: PS: Since JS does not have main (), the node. JS Community has agreed to write the main process in app.js.

The utility module is as follows:

var title = "Hello Node"= {    function(titlename) {        =  TitleName;    },    function  () {        console.log (title);}    }

App.js the test code for the module is as follows:

var module = require ("./utility"); module. Settitle ("Hello cnblog"); module. Output (); Console.log (module.title);

Results:

Node. JS's module is very secure, and each module can only get the exports variable.

Path Module

Provides functionality related to file paths and can be adapted to the environment of the operating system (that is, Windows \ and Unix/).

DEMO (test environment for Windows):

varPath = require ("path"); Console.log (Path.normalize ("C:/users/guan/webstormprojects/nodestudy01/app.js"));//Output:c:\users\guan\webstormprojects\nodestudy01\app.jsConsole.log (Path.join ("C:/users/guan/webstormprojects", "NodeStudy01", "App.js"));//Output:c:\users\guan\webstormprojects\nodestudy01\app.jsConsole.log (Path.dirname ("C:/users/guan/webstormprojects/nodestudy01/app.js"));//output:c:/users/guan/webstormprojects/nodestudy01Console.log (Path.basename ("C:/users/guan/webstormprojects/nodestudy01/app.js"));//Output:app.jsConsole.log (Path.extname ("C:/users/guan/webstormprojects/nodestudy01/app.js"));//Output:.js
FS Module

Provides the ability to read and write files in both asynchronous and synchronous modes.

DEMO:

var fs = require (' FS '); Fs.writefilesync (' testA.txt ', "Hello Node"); Console.log ( Fs.readfilesync(' TestA.txt '). toString ()); Fs.writefile (function  () {    Console.log ( Fs.readfilesync (' testB.txt '). toString ());});
OS Module

This module provides access to the current system OS-related functions, such as: Get CPU cores, memory allowances, etc.

DEMO:

Console.log (' Total Memory ', Os.totalmem (), ' Bytes '); Console.log (' Available memory ', Os.freemem (), ' Bytes ' ); Console.log (' This machine has ', Os.cpus (). Length, ' CPUs ');

Of course, there are many module, specific reference to the node. JS Official API DOC.

node. JS Learning 01 ends, and 02 will introduce content about packages.

node. JS learns 01:module System and some common node Module

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.