Great routine analysis of node. js Reading Notes, node. js Reading Notes
This week, nodejs learned how to use several dependent packages and used all the examples in the book. This article takes the routine as a clue to review the work of the week.
1. connect
This routine mainly uses the connect dependency package. connect provides a middleware (composed of functions that interact with request and response objects) function. The book also introduces other built-in middleware of connect, which can organize code to complete web functions.
2. session
Use connect to perform user sessions and perform basic logon to the system. Middleware allows you to check logon, display forms, match users, and process logout. Demonstrate the strength and organization of middleware.
3. express-tweet
This chapter describes how to use the express package and connect-based express. The program design is still simple, but in different np versions, function names will change. For example, express. createServer () becomes express () after 3.0 (). The biggest benefit of using express is its simplicity and flexibility. However, if tweet cannot be accessed, but is rewritten as weibo, weibo's API access is too complicated. In fact, this program is not successfully run.
4. echo
The two routines use the WebSocket package. The echo function is to record the message transmission time. Websocket uses ws to listen to (on) an event and process it in the callback function. js is also used for message interaction in html files.
5. cursors
In this example, broadcast is learned. This function is processed by a self-written broadcast function. Make sure that the cursor element exists by dom id lookup.
6. chat
This chapter can be said to be the highlight of this book, because the Socket. IO package was developed by the author. It differs from WS in that its message transmission is based on transmission rather than WS. This routine first implements the chat function and then the broadcast Song (DJ) function. There were no major problems in the process. One process is a socket. emit event. The socket at the other end listens to the event and processes it.
7. Summary
This week's efficiency is quite good, and the problems encountered are still on evernote. The difference between localhost and 127.0.0.1 has also occurred before. This week, I learned a function during js debugging, which allows js to dump an object like php. Now paste it below for sharing.
Copy codeThe Code is as follows:
Function dump (arr, level ){
Var dumped_text = "";
If (! Level) level = 0;
// The padding given at the beginning of the line.
Var level_padding = "";
For (var j = 0; j <level + 1; j ++) level_padding + = "";
If (typeof (arr) = 'object') {// Array/Hashes/Objects
For (var item in arr ){
Var value = arr [item];
If (typeof (value) = 'object') {// If it is an array,
Dumped_text + = level_padding + "'" + item + "'... \ n ";
Dumped_text + = dump (value, level + 1 );
} Else {
Dumped_text + = level_padding + "'" + item + "' => \" "+ value +" \ "\ n ";
}
}
} Else {// Stings/Chars/Numbers etc.
Dumped_text = "==>" + arr + "<== (" + typeof (arr) + ")";
}
Return dumped_text;
}
I will start learning mongoDB in the next week.Learning routine, reading Source Code APITo improve. After all, the advantage of node is the np of a large part of the network.