node. js Small Finishing (i)

Source: Internet
Author: User

node. js is a JavaScript runtime environment based on the Chrome V8 engine

The JavaScript code we wrote before was run in the browser, so we could just hit the code in the browser and run it directly. Now learn node. js, the code cannot be run directly in the browser, but run in the node environment

node features and benefits :

Features: Event-driven, non-blocking IO model

Advantages: Light weight and high efficiency

command-line mode and node interaction mode

Command-line mode : see similar c:\> is available in Windows command-line mode, as follows

in command-line mode, you can enter the node file name. JS executes a file

CD//Switch directory CD folder//Enter the target file

Cd.. Return to upper level D://Enter D drive

MD New Folder name//new folder (where the current path points to where the folder is stored, such as c:\user\administrator> MD H new folder h is stored in the Administrator of User in the C drive)

Rd folder name//delete folder

node Interaction mode : See > is in node interactive mode, as follows

in node interactive mode we can enter JavaScript code to execute immediately

The JavaScript code that runs in these two modes is different: the node interaction environment will automatically print out each line of JavaScript code, but running the JavaScript file directly will not, unless you add console.log to print it out.

Two ways to run JavaScript code into the current folder:

    • Locate the current project folder and open-->shift+ right-click here to open the command window-->node+ space + file name. js Run File
    • Windown+r open cmd Command panel--Enter cd+ specified folder (CLS can clear the current screen)

Module system:

A file is a module, the module has scope, in a module of variables and function objects belong to this module, external is not visible. node belongs to modular development

node. JS Common modules:

    • http module   Reference: var http = require (" http ")   function: You can write an HTTP protocol client program (browser), You can also write a server-side program based on the HTTP protocol (Apache)
    • url module   reference: var URL = require ("url")   Function: Convert the part of the URL address to the JS object property
    • querystring module   reference: var querystring = require ("querystring")   function: Convert a query string to an object
    • FS module   Reference: var fs = require ("FS")   function: Read file

Asynchronous Read file : ReadFile (Path,utf8, callback function)

var fs = require ("FS"); Fs.readfile ("Test.js", UTF8,function(err,data) {  // Test.js must be in the current  directory if(err)     {throw  err  ; Else {     console.log (data)  }});

Synchronous Read Files : Readfilesync (Path,utf8)

var fs = require ("FS")var data = Readfilesync ("text.html", UTF8); console.log (data);

Write files asynchronously

fs.writefile ("File path", "Write content", UTF8, (err) =>{  //If the file does not exist, it will automatically create the file  but cannot create the folder
Console.log (err); When the output is NULL indicates success
}

Synchronizing write files

Fs.writefilesync ("File path", "Synchronous write", UTF8)

In the FS module, the synchronization method is provided for ease of use, so whether to use synchronous or asynchronous? The JavaScript code executed by the node environment is the service-side code, so most of the code that needs to run the business logic repeatedly on the server must use asynchronous code

Here's a look at the next few concepts

io: That is, input and output computer inputs and outputs, because the network transmission is a string, IO in the server can be understood as read and write operations

concurrency : There are several programs in a period of time that belong to the state between boot and run completion

There are two types of async: Asynchronous IO and asynchronous non-IO

Asynchronous IO: To understand this concept, first know the process and the thread, the process is the running environment provided by the running application, a running application, at least one process will correspond. threads are used to run code in a program, and a thread can only do one thing in a single time. node is a single-threaded asynchronous operation that uses async instead of multithreading (multithreaded debugging Complex) asynchronous IO operations: Ajax network operation FS

Asynchronous non-IO operation : SetTimeout setinterval (only these two)

node program running process : The main thread has been circulating, know that there is no code in the queue, the program ends the child thread has been online constructor rest, used to execute asynchronous IO operation Sub-thread: A. Online constructor rest b. Asynchronous IO in to resolve asynchronous IO operations C. Child threads callback the results of asynchronous IO operations into the queue d. Continue to rest in the thread

 Event-driven : When node executes code, the code is put into the queue, the event loop program executes the queue synchronization code, and the synchronous code executes before the asynchronous code is executed.

the building of a simple server   

// introduce the HTTP module http.createserver (function// turn on the local HTTP service to listen for the appropriate port  Res.writehead (200 , {"Content-type": "Text/html;charset=utf8"})  res.end ("I am a server");  // The end request can have only one }). Listen (3000);

node. js Small Finishing (i)

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.