No contact, try to learn briefly, start from the beginning;
Refer to this tutorial: https://github.com/alsotang/node-lessons/tree/master/lesson0
first, Build the Environment:
1, build Node. JS Environment seems to be mostly based on Linux system, Windows system seems to be unpopular, no way, on Windows to install a CentOS system;
2. Install NVM (Node Version Manager), Follow the above tutorial, enter the command: (you should restart the Terminal)
3, Install Node. js; Restart the terminal, also follow the tutorial install 0.12 this version;
Installing ... There is a progress display; I went to the card 82.1%, five minutes! Had to restart the terminal, continue to download the completed; (when the card is restarted?) )
4, installation completed, Start node; tried, as if to first enter the NVM use 0.12 command; The original is not run $ NVM alias default 0.12 this command;
After setting the default Value:
The simplest example of second:
1, first need to use the NPM (Node package Manager) to install a framework express; do not understand, first follow the tutorial to:
Http://registry.cnpmjs.org This address was banned from the intranet, reported the network Error. So change a try http://registry.cnpmjs.org;
2. Start writing JS code, implement a server, port 3000, access address http://localhost:3000
//the meaning of this sentence is to introduce the ' express ' module and give it the ' express ' variable for Use. varExpress = Require (' Express ');//call the Express instance, which is a function that, when called without arguments, returns an express instance that assigns this variable to the app Variable. varApp =Express ();//the app itself has many methods, including the most commonly used get, post, put/patch, delete, where we call the Get method and specify a handler function for our '/' path. //This handler function will receive the REQ and res two objects, which are requested request and response Respectively. //the request contains various information from the browser, such as query, body, headers, etc., can be accessed through the Req object. //Res object, We generally do not take information from the inside, but through it to customize the information we output to the browser, such as header information, such as the content you want to output to the Browser. Here we call its #send method, outputting a string to the Browser. App.get ('/',function(req, res) {
When there are parameters, Http://localhost:3000/?q=niu is obtained with the following code;
var param = req.query.q;
If (param==null | | param = = ' undefined ') {
Res.send (' Why no params,go put one; ');
//}
Res.send (' Hello ' +param);
res.send (' Hello World ') ; // after defining the behavior of our app, let it listen to the local 3000 port. The second function here is a callback function that executes after the listen action succeeds, and here we execute a command-line output that tells us that the listener action is Complete. function () { console.log (' app is listening at Port ');});
//Introducing DependenciesvarExpress = Require (' Express ');varsuperagent = require (' superagent '));varCheerio = require (' cheerio '));//Create an Express instancevarApp =Express (); App.get (‘/‘,function(req, res) {//use superagent to crawl the contents of http://www.cnblogs.com/Superagent.get (' http://www.cnblogs.com/'). End (function(err, sres) {//General error Handling if(err) {returnNext (err); } //Sres.text stores the HTML content of the Web page and passes it to Cheerio.load //you can get a variable that implements the jquery interface, and we habitually name it ' $ ' //all That's left is Jquery's content. var$ =Cheerio.load (sres.text); varItems = []; $('. Titlelnk '). each (function(idx, Element) {var$element =$ (element); Items.push ({title: $element. text (), href: $element. attr (' href ') }); }); $('. LightBlue '). each (function(idx, Element) {var$element =$ (element); Items[idx].author=$element. Text (); }); Res.send (items); });}); App.listen (3000,function(req, res) {console.log (' app is running at Port 3000 ');});Crawl cnblogs.com article title
Execution succeeded using the node App.js command:
third, try to Web crawler, heard very *: this time to write to some details:
1. Create new folder and Open: mkdir nodesprider && cd Nodesprider
2, The instantiation Project establishes PACKAGE.JSON:NPM Init to generate the package.json;
3, install three dependencies and configuration information to package.json: NPM install Express superagent Cheerio--save by default from the official website download dependence;
Express (http://expressjs.com/) is the most widely used web framework for Node. JS and is now in the 4.x version, which is very thin.
Superagent (http://visionmedia.github.io/superagent/) is a library of HTTP aspects that can initiate a GET or post Request.
Cheerio (https://github.com/cheeriojs/cheerio) can be understood as a Node. JS version of jquery, used to extract data from a Web page in CSS selector, using the same way as Jquery.
4. Crawler Logic Code:
Introducing Dependencies
var express = require (' Express ');
var superagent = require (' superagent ');
var cheerio = require (' cheerio ');
Create an Express instance
var app = Express ();
App.get ('/', function (req, res) {
Use superagent to crawl the contents of https://cnodejs.org/
Superagent.get (' Https://cnodejs.org/')
. End (function (err, sres) {
General error Handling
If (err) {
Return next (err);
}
Sres.text stores the HTML content of the Web page and passes it to Cheerio.load
You can get a variable that implements the jquery interface, and we habitually name it ' $ '
All That's left is Jquery's content.
var $ = Cheerio.load (sres.text);
var items = [];
$ (' #topic_list. topic_title '). each (function (idx, element) {
var $element = $ (element);
Items.push ({
Title: $element. attr (' title '),
Href: $element. attr (' href ')
});
});
Res.send (items);
});
});
App.listen (function (req, res) {
Console.log (' app is running at Port 3000 ');
});
5, the implementation of App.js:node app.js; visit: http://localhost:3000/; Browser Output information! It worked
Learning server-side JavaScript this famous Node. JS