JS Learning Summary----A preliminary understanding of node

Source: Internet
Author: User

JS is a "lightweight" scripting language that "runs in the client (running in the browser)"

JS is now more than just running in a browser, it can also run in node

Node's advantages: Based on the Google V8 engine, it has powerful IO operations, event-driven asynchronous programming.

1. What is node

node is an environment for JS Code Execution Environment, we can put him in the equivalent of the browser, but we will generally put node this environment to the server side, so we can use JS programming on the server side, that is, JS is not only the language of the client, but also the server-side language ...

2. Node && browser

Node uses Google's V8 engine to render JS (running fast, stable, we write the JS code does not need to consider compatibility)

The global JS object in the browser is window, and the global JS object in the node environment is

The browser is installed on the client, in order to protect the client's security, it is basically impossible to provide JS to the client computer disk files on the function of the operation, but the node environment provides the corresponding I/O operations (file on the server operation), we use JS can be used in the server disk files to be added and censored.

Node is event-driven/asynchronous Programming (the JS program we write in the node environment is generally asynchronous programming)

Node offers many new ways to JS: Http.createserver, Fs.writefilesync, Fs.readfilesync ...

3, how to let JS code in node execution?

Using Webstrom embedded node environment, directly write JS through the right button "run Xxx.js" in the node environment (not recommended that you often use this way, because sometimes my most recent operation will actually run the previous cache, because there will be a cache)

In the command window, execute the

In the current directory of JS files to be executed, the shift+ right button = = Opens the command window here and Node Xxx.js is equivalent to the JS code executed in the node environment.

In node execution JS code and browser execution, if the JS code changes, want to see the latest effects need to re-execute the corresponding JS file.

Execute node in the command window and start writing the corresponding JS code test (Rpel command action), just as you would write code tests in the browser's console.

4. Node module

  built-in modules (node environment is inherently provided): http (createserver ...), FS (Writefilesync, readfilesync), url ...

  Custom Modules : our own defined modules

In the node environment, we create a JS file under the current project, which is equivalent to creating a module, for example: Create a new a.js, which is equivalent to creating a module

If you need to provide some way for other modules to use, you need to export the method through Module.exports, for example: Write in a module module.exports = {FN:FN}/Module.exports.fn = fn

If now used in the B module, we need to first import var a = require ("./a") so that you can adjust the method of the A module export A.fn ()

  third-party modules : The modules that others have written we use in node if you need to use someone else's module, you need to use the NPM command to manage https://www.npmjs.com/

Installation: NPM install third-party module name-g (installed in node's global environment)

Uninstall: NPM Uninstall third-party module name-G

After the installation is successful in JS through var less = require ("not"), the third-party module just installed into JS, so we can use the method provided in this module Less.render ...

5. How to send a request to the server after the service has been created successfully

Enter http://localhost in the browser: port number/

In the browser, enter the IP address of http://native, port number/

  

Here is the code for common HTTP, FS, and URL methods:

//import three common node built-in modulesvarHTTP = require ("http"), FS= Require ("FS"), URL= require (' URL ');//The URL module provides a method, Url.parse (str), to parse the URL address. The first parameter is the passed URL, the second parameter is false by default, and the query that returns the result stores the processed parsed result when it is true: Store the multiple sets of data that are passed in as key-value pairs//1. HTTP/*Http.createserver Create a service, variable server is the service that we created Server.listen: Listen to a port for this service*/ varServer = Http.createserver (function(request,response) {//when the client sends a request to the server-side current service (the port number is 80), and the current service has successfully received the request    //Request : Holds the request information for all clients, including the data content that the client passes to the server through the question-mark argument    //Console.log (Request.url);//The address requested by the client, which is obtained by the server through Request.url, is not including localhost or IP address information such as: index.html?name= Zhangsan&age=7    varUrlobj = Url.parse (Request.url,true), pathname=urlobj.pathname, Query=Urlobj.query; //gets the source code in the corresponding resource file based on the requested URL address (specifically, based on the pathname in the address)    if(Pathname = = = "/1.html"){        //Fs.readfilesync ([Path+name],[encode]) reads the contents of the specified folder synchronously: The contents of the file are not read and do not perform the following actions        varcon = Fs.readfilesync ('./1.html ', "Utf-8"); }        //Response (Response): Provides a way to return content and data to the client    //Response.Write: Returning content to the client    //Response.End: Tell the server that the response is over (be sure to add it)Response.Write (Con); Response.End ();}); Server.listen (80,function(){    //This callback function is executed when the service is created successfully and the port number is successfully monitoredConsole.log ("Server is create success,listening on port")});

JS Learning Summary----A preliminary understanding of node

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.