node. js Authoritative Guide Notes

Source: Internet
Author: User

Chapter One introduction to node. js

1. Modules in node. js

Each module is a JS file, the scope of the global variable or function defined by each module is also scoped to the module, and only the exports object can be used to pass it to an external

2, simple example:

var http = require (' http '); http.  Createserver(function  (req, res) {   res.writehead($, {' Content-type ': ' Text/html '});  Res.write (' );  Res.end (' Hello \ n ');}). Listen (1337, "127.0.0.1"); Console.log (' Server running at http://127.0.0.1:1337/')

3. The core Module table in node. js

4. Classes, functions, and objects appended in node. JS (that is, except for functions and classes defined in ES5)

Chapter II Interactive Runtime Environment in node. JS-REPL

1. Use the Eval function inside the REPL environment to evaluate the expression is the result of the execution

Console.log (foo = ' bar ');   // BarConsole.log (var foo = ' bar ');  // undefined

2, Repl Use the underscore "_" to access the most recently used expressions

3. Accessing the context in the REPL environment

var repl = require ("REPL"); var con=repl.start (">"). Context;con.msg= "sample Message"; con.testfunction=function () {console.log (con.msg);};

4. Basic command

. break C = "TRL + C"

Two times Ctrl + C EXIT REPL Environment "=". Exit

. Clear (Clears all variables and functions in the context environment)

. Help

. Save As

. Save. /save.js

. Load loads all expressions sequentially 

Chapter three basic knowledge of node. js

1. Console object in node. js

2. Console.log method

// app.js Code console.log ("This is a test string");

// Console input string is entered into the Info.log file (1 for redirect standard output stream)

Console.log can also output string formats by specifying parameters

// Hoge fooconsole.log ("%d", 10,10.5);  // Ten 10.5Console.log ("%d", "Hoge", "foo");  // NaNconsole.log ("percent", "Hoge");  // % Hoge

3. Console.log method and Console.info method (consistent usage)

4. Console.log Method (Console.warn)

// app.js Code console.error ("This is a test string"); // Console input string is entered into the Error.log file (2 for redirection standard error output stream)

5. Console.dir method: View the contents of an object and enter it into the console

var user=New  Object (); User.Name= "Lulingniu"; user.getname=function() {returnthis. name;}; User.setname=function(name) {this. name=name;}; Console.dir (user);

6, Console.time and Console.timeend

Console.time (' small loop ');  for (var i = 0; i < 100000; i++) {  ;} Console.timeend (' small Loop '); // output: Small loop 2ms

7. Console.trace method (outputs the stack information at the current location as a standard error message)

var user=New  Object (); User.Name= "Lulingniu"; user.getname=function() {returnthis. name;}; User.setname=function(name) {this. name=name;}; Console.trace (' trace ');

8, Console.assert Method: Used to evaluate the execution of an expression, if the value of the expression is false, output a message string and throw a Assertionerror exception

Console.assert (1=22, "raise an exception");

9, Console.log (global); To view the contents of the global object in Nodejs

node. js Authoritative Guide Notes

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.