"Nodejs Study" the first day of 0.nodejs learning

Source: Internet
Author: User
Tags install node

1. Module

Larger programs require modularity, Nodejs is no exception, the code is placed in a different file, each file can be a module, the file path name is a module name. Each module contains three pre-defined variables:

1.require: for loading and using other modules in the current module, passing in a module name, returning a module export object, ". js" can be omitted.

    Eg:var fool1 = require ('./foo ');//.foo.js,/home/user/foo

    & nbsp;      var data = require ('./data.json ');//load JSON file

2.exports: Current Module Export object, Common methods and properties for exporting modules, other modules using the current module through require is the exports object of the current module.

    Eg:exports.hello = function () {

                conscole.log (' Hello world! ');

          }

3.model: You can access some relevant information for the current module, with the most purpose of replacing the current export object. A normal object when the module exports an object, if changed to a function, you can:

    eg:module.exports = function () {

   & nbsp;            conscole.log (' Hello world! ');

          }

2. Module initialization

The JS code in a module is executed only once for the first use, and the exported object of the module is initialized during execution, after which the exported object is cached for reuse.

3. Main module

Used to start the program, which is equivalent to the main function in the C language

Counter.js

var i = 0;

function count () {

return i++;

}

Exports.count = count;

Main.js

var counter1 = require ('./counter.js ');

var counter2 = require ('./counter.js ');

Console.log (Counter1.count ());//0

Console.log (Counter2.count ());//1

Console.log (Counter2.count ());//2

$ node Main.js

0

1

2

Why not? Please think for yourself, and some of the information on the web is

4. Module path parsing rules

1. The built-in module does not do path parsing directly returns an internal module export object. Eg:require (' FS ');//fs and HTTP are built-in objects

2.node_modules directory: This special directory is used to store modules, such as the absolute path of a module is/home/user/hello.js, using require (' Foo/bar ') mode load, then Nodejs try:

/home/user/node_modules/foo/bar

/user/node_modules/foo/bar

/node_modules/foo/bar

3.node_path, when you use require (' Foo/bar '), the directories in Node_path are loaded at once.

5. Package

The files in the package are placed in a directory, and all the sub-modules of the package require an ingress module, and the exported object of the Ingress module is used as the export object of the package.

If the module file name is Index.js, you can use the file path to omit the index using the module, so that you can require a package, more overall sense.

6.package.json

If you want to customize the file name and location of the Ingress module, you need to include a Package.json in the package directory and specify the path to the Ingress module.

- /home/user/lib/    - cat/        + doc/        - lib/            head.js            body.js            main.js        + tests/        package.json
//package.json
{    "name": "cat",    "main": "./lib/main.js"}
require(‘/home/user/lib/cat‘)方式可以加载模块
7.命令行程序
Linux
1.添加注释:#! /usr/bin/env node
2.添加执行权限:chmod +x /home/user/bin/node-echo.js
3.在path中指定路径:sudo ln –s /home/user/bin/node-echo.js /user/local/bin/node-echo
可以在任何目录下使用node-echo命令了
Windows
1.PATH中配置C:\Users\users\bin目录
2.在C:\Users\users\bin目录下新建node-echo.cmd文件,文件内容为:@node "C:\User\user\bin\node-echo.js" %*
可以在任何目录下使用node-echo命令了
8.工程目录
- /home/user/workspace/node-echo/   # 工程目录    - bin/                          # 存放命令行相关代码        node-echo    + doc/                          # 存放文档    - lib/                          # 存放API相关代码        echo.js    - node_modules/                 # 存放三方包        + argv/    + tests/                        # 存放测试用例    package.json                    # 元数据文件    README.md                       # 说明文件

Some of these files

/* bin/node-echo */var argv = require(‘argv‘),    echo = require(‘../lib/echo‘);console.log(echo(argv.join(‘ ‘)));/* lib/echo.js */module.exports = function (message) {    return message;};/* package.json */{    "name": "node-echo",    "main": "./lib/echo.js"}
上面这些代码还不能执行,还没有完成,不要着急。
9.NPM
$ npm install argv
或者是在package.json中添加
"dependencies":{
    "argv":"0.0.2"
}
在工程目录下使用npm install命令批量安装三方包,以后node-echo也上传到服务器,别人下载这个包,会根据申明的第三方依赖自动下载进一步依赖的三方包,使用npm install node-echo命令,自动创建一下目录

Appendix:

1.NPM adduser: New user

2.NPM Publish: Release code

3.NPM Help:

NPM help command name, opening HTML helper

4. In the directory where Package.json is located, use NPM install. -G to install the current command-line program locally for pre-release testing

5.NPM update <package>: Update module

6.NPM Update <package>–g: Global update

7.NPM Cache clear: Empty NPM local Caches

8.NPM Unpublish <package>@<version>: Undo code for a version that has been published

These as a preparation for knowledge, and then continue to learn

"Nodejs Study" the first day of 0.nodejs learning

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.