Learning Nodejs, for the front-end personnel, do not understand the background technology, is not too easy. Of course, the only benefit of Nodejs seems to be the same style of JavaScript as its code compilation, which, in other words, is written in the background code through JavaScript. This is a new breakthrough and innovation for the JavaScript language (the innovation is a bit big, the breakthrough is affirmative) hehe.
Since many front-end people do not understand the background language, learning Nodejs, it is necessary to learn to build a complete project framework through the Open source Framework Express under Nodejs.
Express is a nodejs in the open-source framework, can be said on the Internet, there are many predecessors through their own examples of how to build a project framework through Express. Guide the front end "pure" how to build a framework with HTTP server code and routing router code ( front-end data request Ajax, follow a route corresponding to a request ). Clarified the front-end "pure" how to manage their own code in the development of separate projects-especially the "pure" front-end code and back-end code. Told that as the front-end "pure", all need to do and maintain their own code, to be able to let their projects run up!!
Of course, you must download and install the configuration Nodejs development environment to learn NODEJS or to develop projects independently through NODEJS. These include node. js, NPM, and Express. Features such as NPM and configuration files are included in the new Nodejs installation package, so there's an open door for node novices. Just on the NODEJS official website
http://www.nodejs.cn/or http://nodejs.org/ directly download the latest version, follow the installation process simple operation to complete the NODEJS development environment configuration.
The rest is to learn how to develop projects in the node environment. Of course, it is best to learn the functions of node-included modules and packages in some beginner tutorials.
NPM, as node's package Manager, is in a detached position. Learn to install and invoke the required packages by command. The use of Express is to build the project framework by order.
Steps:
1. In the root directory of the disk where the project is located, execute express-e myProject through the cmd Command window to create the MyProject folder (the-e in the command is the abbreviation for the file name Ejs, and if it is not, Jade is the default) as the project and file. The basic framework content required for the project is automatically configured in the MyProject file. And then go through NPM.
The install command configures the package required for the project, and so on.
2. After the project Principal directory framework is set up, the rest is code writing and server and routing configuration issues. It is important to note that the output of the node module or package (exports.xxx = xxx; or module.export = XXX) and the introduction (var xxx = require ("./filename also called module name"); xxx.xxx (); or xxx ();).
Paste the following code:
Because the express new directory when the command has the-e so the view folder under the file suffix named. ejs.
So need to edit views under Index.ejs. Add the code in the change file <body>:
Front-End Code:
<div class= "Showmess" ></div>
<script src= "Http://cdn.bootcss.com/zepto/1.0rc1/zepto.min.js" ></script>
<script>
$.ajax ({
Data:{page:1},
DataType: "JSON",
Type: "Get",
URL: "/router",
Success:function (data) {
//console.log (data)
var html = "";
for (Var i=0;i<data.blogs.length;i++) {
var blog =
Data.blogs[i];
html + = "<p><a href= '" +blog.src+ " target= ' _blank ' > ' +blog.title+ ' </a><br> '
+ "<div class= ' break ' >" +blog.content+ "</div><div class= ' moremess ' style= ' text-< /c5>align:right ' > ' +
blog.time+ "+blog.read+" "+blog.say+" </div></p> "
}
$ (". Showmess"). HTML (HTML);
},
error:function () {
Alert ("Error")
}
})
</script>
Back-End Code:
Under the routes file can create a new custom JS file, I will be named Creeper.js. The contents are:
var http = require ("http"),
Cheerio = require ("Cheerio");
Exports.getblog = function (req, res) {
var page = req.param ("page") | | 1;
var _res = res;
http.get (' http://www.cnblogs.com/axes/default.html?page= ' +page, function(res) {
var chunks = [],
size = 0;
Res.on ("Data", function (chunk) {
Chunks.push (chunk);
Size + = Chunk.length;
});
Res.on ("End", function () {
Splicing buffer
var data = Buffer.concat (chunks, size);
var html = data.tostring ();
var $ = cheerio.load (HTML);
var blogs = [];
For (Var i=0;i<$ ('. PostTitle2 '). length;i++) {
var blog = {};
Blog.title = $ ('. PostTitle2 '). EQ (i). text ();
BLOG.SRC = $ ('. PostTitle2 '). EQ (i). attr ("href");
Blog.content = $ (". C_b_p_desc"). EQ (i). text ();
var mess = $ (". Postdesc"). EQ (i). Text (). Split ("<a") [0].split ("");
Blog.time = mess[2]+ "" +mess[3];
Blog.read = mess[5];
Blog.say = mess[6];
Blogs.push (blog);
}
_res.json ({
Blogs:blogs
})
})
}). On (' Error ', function (e) {
Console.log ("Error:" +e.message)
});
};
Note of the Exports.getblog value in the file.
Then in the establishment of any custom JS file, I will be named Router.js. The contents are:
var http = require ("http");
var cheerio = require ("Cheerio");
var express = require (' Express ');
var router = Express. Router ();
var creeper = require ("./creeper.js");
/* GET home page. */
Router.get ('/', creeper.getblog);
Router.get ('/index ', function (req, res) {
Res.render (' index ', {title: ' Tobi '});
});
Module.exports = router;
This file is for the introduction of a routing file (that is, the contents of the Creeper.js file created above exports) to get back
Back data ( a route corresponds to a request ) and the Index.ejs file under the views file for front-end rendering.
The following is the configuration of the corresponding route in the App.js file,
var router = require ('./routes/router ');
App.use ('/router ', router);
We will see the WWW file under the bin file, which defines the server port number as 3000.
After clearing the above file and code, we need to open the Command window under the project root file to start the service directly.
Start Service Command: node start
The next step is to access Port 3000 locally.
Http://127.0.0.1:3000/index
All of the above are small-size brother after learning node, through the Express framework of small-instance web crawler. is to learn from predecessors
W. Axes's blog post is a string of his own understanding.
Disclaimer: This article is not reproduced, if you need to reprint please indicate the source, thank you.
Learn from Blog: http://www.tuicool.com/articles/67zYfiy
Original: http://www.cnblogs.com/axes/p/3668051.html
This article is from the "focus on Technology focus Front" blog, be sure to keep this source http://oxoxo.blog.51cto.com/9301862/1765133
First entry Nodejs Express frame