Nodejs Getting Started

Source: Internet
Author: User

I. Introduction of NODEJS

Simply put, node. JS is the code parser that runs JavaScript on the server.

Website Introduction:

 for easily building fast, scalable network applications. node. JS uses an event-driven, non-blocking I/O model that makes it lightweight and efficient, perfect for data-intensive R Eal-time applications that run across distributed devices.
node. JS is a platform built on the chrome JavaScript runtime, which in fact encapsulates the Google Chrome V8 engine, which is used primarily to create fast, scalable Web applications. node. JS uses an event-driven and non-blocking I/O model to make it lightweight and efficient, making it ideal for building data-intensive, real-time applications running on distributed devices.

1. Node. JS is a platform, or a running environment.

2. Node. JS is a platform built on the Chrome JavaScript runtime, which is based on the Chrome V8 engine, and the V8 engine executes JavaScript very quickly and with great performance.

3. Node. JS is designed to provide a simple way to build fast, scalable network programs.

4. Using data-driven, non-blocking IO models

5, lightweight, scalable, suitable for real-time data-intensive interactive applications.

6. Single process, single thread

JavaScript, which runs in the browser, is the parser for JavaScript code, and node. JS is the server-side JavaScript code parser that exists on the server-side of JavaScript code that is parsed and run by node. js.

The JavaScript parser is just an environment in which JavaScript code runs, and the browser is an environment where JavaScript operates, and the browser provides JavaScript with interfaces for manipulating Dom objects and window objects. node. JS is also an environment where JavaScript runs, andnode. JS provides JavaScript with an interface for manipulating files, creating HTTP services, creating TCP/UDP services , and so on. So node. JS can do the work that other background languages (Python, PHP, etc.) can do.

Second, the implementation of the environment

NODE.J provides an interactive runtime environment Repl that allows you to execute JavaScript blocks immediately, using the same method as the console of the Firebug plugin in the Chrome browser.

Enter node or node. js into the interactive runtime environment of node. js. Ctrl+d exit.

1. Execute JAVASCRITP code block through interactive environment

$ node> console.log (' Liuxiaoyan ');

2. Running JavaScript files through an interactive environment

$ node Test.js
Iii. node. js modules and packages 1, modules

A module is a file.

Each node. js is a node. js module, including JavaScript files (. js), JSON text files (. json), and binary module files (. node).

node. JS officially provides a number of modules that implement specific functions, such as module FS for manipulating files, and module HTTP for building HTTP services.

You can use the official module or write it yourself.

2. Package

A package is a folder that organizes multiple dependent modules together to encapsulate multiple modules for easy management.

node. JS has a COMMONJS specification, and the package must contain a JSON file named Package.json.

The Package.json should contain the following fields:

Name: Package name. The package name is unique and can contain only lowercase letters, numbers, and underscores. Version: the package revision number. Description: Package description. Keywords: key word group. For search. Homepage: Project home page. Bugs: The address where the bug was submitted. License: License. Maintainers: Maintainer array. Contributors: Contributor Array. Repositories: An array of project warehouse managed addresses. Dependencies: Package dependency.

Example:

{    "Name": "Shiyanlou",    "description": "Shiyanlou Test Package.",    "Version": "0.1.0",    "keywords": [        "Shiyanlou",        "Nodejs"     ],    "Maintainers": [{        "Name": "Test",        "Email": "[email protected]"    }],    "Contributors": [{        "Name": "Test",        "Web": "http://www.shiyanlou.com/"    }],    "Bugs": {        "Mail": "[email protected]",        "Web": "http://www.shiyanlou.com/"    },    "Licenses": [{        "Type": "Apache License v2",        "url": "Http://www.apache.org/licenses/apache2.html"    }],    "Repositories": [{        ' type ': ' Git ',        "url": "Http://github.com/test/test.git"    }],    "Dependencies": {         "WebKit": "1.2",        "SSL": {             "GnuTLS": ["1.0", "2.0"],            "OpenSSL": "0.9.8"        }    }}
View Code

The Bin folder holds the binaries.

Lib folder to store JavaScript files.

Doc folder to hold the document.

Test folder to store unit tests.

3. NPM Package Management tool

NPM is a package management tool for node. js, and NPM defines the package dependency criteria that NPM uses primarily to download and manage third-party packages.

Common commands are as follows:

Search for third-party packages:

$ sudo npm search Express

Installation package:

$ sudo npm install-g Express

Update package:

$ sudo npm update Express

To uninstall a package:

$ sudo npm Uninstall Express
Iv. notation and invocation of the module 1, notation 1, and calling Method 1

The Mymodule.js code is as follows, providing interfaces externally through module.exports and exports.

function Hello () {    console.log (' hello ');} function World () {    console.log ("World"== world;

Called in Index.js

var // you can also write var hello = require ('./mymodule.js '); // Now you can use the functions in Mymodule.js. // >> Hello // >> World
1, notation 2 and call Method 2
function Hello () {    thisfunction() {        console.log (' Hello ');    };      This function () {        console.log (' World ');     = Hello;

Called in Index.js

var Hello = require ('./mymodule '); var New  //  >> Hello//  >> World
V. Module.exports and Exports

Module is an object, each module has a modules object, and module is a reference to the current module.

The Module.exports object is created by the module system, and exports can be thought of as a reference to the Module.exports object.

when require another module in a module, the value of Module.exports is the same, because in some cases, module.exports and exports their values are different.

 //  Module.exports and exports in the same situation  Span style= "color: #0000ff;"        >var  m = {}; //  means the module   var  e = M.E = {}; //  E represents exports, M.E represents module.exports  m.e.a  = 5;e.b  = 6  ;console.log (M.E);  //  Object {a:5, b:6}  Console.log (    e); //  Object {a:5, b:6}  
// Module.exports and exports different situations. var m = {};        // represents the module var e = M.E = {};  // e represents exports, M.E represents Module.exports  = {C:9};    // M.E (Module.exports) referred to the object was changed E.D = ten; Console.log (M.E);   // Object {c:9}Console.log (e);    // Object {d:10}

The author starof, because the knowledge itself in the change, the author is also constantly learning and growth, the content of the article is not updated regularly, in order to avoid misleading readers, convenient tracing, please reprint annotated source: http://www.cnblogs.com/starof/p/5035208. HTML has a problem welcome to discuss with me, common progress.

Reference:

Http://www.xprogrammer.com/159.html

Nodejs Getting Started

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.