node. JS Notes

Source: Internet
Author: User
Tags install homebrew readfile install node

In the node environment to run JS code, JS equivalent to php,node equivalent Apache environment

The first step is to install the node environment
1. Download dmg file installation from official website
2, through the command line installation need to use homebrew (Mac dedicated to manage the software package Manager is also called the warehouse, equivalent to the Yum repository on Linux)
First step: Install Homebrew-"RUBY-E" $ (curl-fssl https://raw.githubusercontent.com/Homebrew/install/master/install)
Step Two: Enter BREW-V to verify if the installation was successful
Tip: If you upgrade your Apple Computer, it causes the Ruby environment to be upgraded, homebrew cannot be used, and you need to modify the homebrew configuration file (/USR/LOCAL/LIBRARY/BREW.RB)
Step Three: Install the node environment through brew (the node environment relies on other environments, such as Git, and so on, it is simple to configure node on a Mac computer, just install Xcode)
Fourth Step: Brew Search Software
Brew Install installation software
Brew List view all software currently installed through brew
Fifth step: Command line Input Brew install node installation environment
Note: The node environment, developed by Google, uses the V8 engine to parse the JS code, is currently the fastest resolution engine

Developing node. js
I. Introduction to the HTTP module
Introduction of an HTTP module
var http = require ("http");

var server = Http.createserver (function (req,res) {
Request:http Request Object (contains some information such as URL, host name, etc.)
Response:http Response Object (the object that the server returns to the client)

Res.end ("Hello"); After the server returns the client data, it ends the secondary link and can also be used to return the data to the client, but there is a limit to return only one piece of data. Use write to return more than one piece of data. The Write parameter only accepts strings
});

Server.listen (9999);

Second, the Requset object, inside the commonly used attributes
URL: '/', '/' represents the root directory of the current server
Method: ' GET ',
Third, Response object
Four, the background can directly output the front-end code
Five, Small summary: node advantage
Front-End developer perspective: 1, low learning cost 2, plug-in management convenience
The advantages of language and the environment itself:
1, the use of Google V8 engine to deal with JS, fast
2. Node. js is the best language to handle concurrency at the moment, completely asynchronous, which is clearly different from the traditional multithreaded development
3. There is no deadlock problem
4, the current major major hardware manufacturers began to support node
Six: Asynchronous processing, callback functions
1, the System Modular Processing: node encapsulated a lot of modules for developers to use, through the introduction of a module require, common with HTTP module, URL module, FS module ....
2, asynchronous processing performance node code inside, (node inside almost all of the callback function takes the way of asynchronous processing)
Fs.readfile ("1.txt", "Utf-8", function (Error,data) {
Console.log (data);
});
Console.log ("222222222222");

The code above will print 222222 before printing data

Introduction of the Simple module
In Webstorm, you can view the source code of any module.
Viewing mode: Command + module name

1. FS module: A module that is used to process files in node. js
Asynchronous operation---Non-blocking operation
Fs.readfile ("1.txt", "Utf-8", function (Error,data) {
Console.log (data);
//});

Synchronous operation-------Blocking operation
var data = Fs.readfilesync ("1.txt", "utf-8");
Console.log (data);
2, the URL module, the most typical example, used to extract the URL to pass the parameters
Request Address: http://127.0.0.1:10000/index.js?name=gxm&pass=123

var http = require ("http");
Introducing the URL module
var Modul_url = require ("url");
var server = Http.createserver (function (req,res) {
Use the URL module to parse the URL of the request object to get useful parameters
var query = Modul_url.parse (req.url,true);

var name = Query.query.name;
var pass = Query.query.pass;

Console.log (name);
Console.log (pass);
Res.end ("Hello");
});
Server.listen (10000);
Viii. introduction of REPL
Full name: Read eval print loop
Chinese meaning: Interactive interpreter

Nine: Mac computer terminal How to start the node. js file
Locate the directory where the JS file is located, the CD command switches to that directory, and then use the
Node file name to run the JS file
Quit using: Ctrl + C to exit

Ten: NPM (similar to homebrew), NPM is a package manager that manages the node environment
NPM install third-party names for third-party packages outside of node
NPM Uninstall third-party name to uninstall

node. JS 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.