node. js Beginner the next day

Source: Internet
Author: User

First, the module

Each JS file is a module, multiple JS files call each other together to implement a function, also known as a module. So: The module is the encapsulation of some functions

1 "JS files and JS files have two modes of cooperation:

1, a certain JS file provides a function for others to use, just to expose the function on the line. Used in combination with exports and require;

2. A class is described in a JS file. The name of the module.exports= structure function;

2 "Node_modules folder:

node, when the introduction of the package, if you do not write "./", the default is to refer to the corresponding JS file from Node_modules, Eg:var foo=require ("Foo.js"), similar to: Var Foo=require ("./node_modules /foo.js ");

Do not write ". js", the default refers to the Node_modules folder under the subfolders of the index.js file; Eg:var foo=require ("foo"); That is: Refer to the Index.js file under the Foo folder under the Node_modules folder;

3 "Package.json File:

Each module folder, recommended to write a Package.json file, the name of this file can not be changed. Node will automatically read the configuration inside. There is a main entry, which is the entry file:

{

"Name": "Kaoladebar",

"Version": "1.0.1",

"main": "App.js"

}

Package.json file, go to the root directory of the module folder.

Second, NPM (node package management)

engineers encapsulate some of the usual functions into packages and put them in the community NPM (https://www.npmjs.com/) for others to use;

How to use: Find the package to use in https://www.npmjs.com/, enter the command in CMD: NPM install package name

When referencing in code, use require directly ("package name");

eg

var silly=require ("Silly-datetime");
Console.log (Silly.format (New Date (), "yyyymmddhhmm"));

Third, the path

_dirname: The absolute path (when accessing the module, write the relative path directly, but when accessing the file, such as TXT, you need to write the absolute path);

require () Other JS file, the JS will be executed. file.

require () in the path, is from the current JS the papers set out to find someone else. and FS is to find someone from the command prompt.

So, there is a a.js on the desktop , the test folder has b.js, c.js, 1.txt

A To reference B:

var B = require ("./test/b.js");

B To reference C:

var B = require ("./c.js");

However, when other modules such as FS are used in the path, they are relative to the position of the cmd command cursor.

Therefore, in b.js to read 1.txt files, it is recommended to use absolute path:

Fs.readfile (__dirname + "/1.txt", function (Err,data) {

if (err) {throw err;}

Console.log (Data.tostring ());

});

node. js Beginner the next day

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.