This week's nodejs learning is about the use of several dependent packages, knocking over the examples in the book. This article takes a routine as a clue and review the work of the week.
1.Connect
This routine uses the connect dependency package primarily, andconnect provides the functionality of a middleware (composed of functions that interact with requests and response objects). The book also describes connect 's built-in middleware, which plays the role of organizing code to accomplish Web functionality.
2.Session
Use connect for a user session and a basic login system. The function of checking login, displaying form, user matching and processing logout is realized through middleware. Demonstrates the power and organization of middleware.
3.Express-tweet
This chapter has learned express based on np , the function name will have some changes, such as express () . Using tweet not accessible, while rewriting Cheng, api access is too complex, and the program is actually not running successfully.
4.Echo
These two routines use the WebSocket package, andEcho 's main function is to record the time of the message transmission. Websocket 's way of flirting with ws to listento an event, in the callback function processing, in the HTML file also use JS for message interaction.
5.Cursors
This example learns to broadcast, and this function is handled by a broadcast function that is written by itself. When the cursor element is ensured to exist, it is done through the DOM 's ID lookup.
6.Chat
This chapter can be said to be the highlight of the book, because the Socket.io package is developed by the author. It differs from WS in that the message passing with it is based on the transmission and not all ws. This routine realizes the chat function first, then realizes the broadcast song (DJ) function. No major problems were encountered during the process. One process is to Socket.emit an event, and the other end of the socket hears the event and handles it.
7. Summary
The efficiency of this week is still pretty good, the problem is still put on Evernote. The problem has also appeared before is the difference between localhost and 127.0.0.1. This week in debugging JS Process learned a function, you can let JS like PHP dump an object. Now post it below to share.
1 functionDump (arr,level) {2 varDumped_text = "";3 if(!level) level = 0;4 5 //The padding given at the beginning.6 varlevel_padding = "";7 for(varj=0;j<level+1;j++) level_padding + = "";8 9 if(typeof(arr) = = ' object ') {//array/hashes/objectsTen for(varItemincharr) { One varValue =Arr[item]; A - if(typeof(value) = = ' object ') {//If It is an array, -Dumped_text + = level_padding + "'" + Item + "' ... \ n"; theDumped_text + = Dump (value,level+1); -}Else { -Dumped_text + = level_padding + "'" + Item + "' + =" "+ Value +" \ "\ n"; - } + } -}Else{//stings/chars/numbers etc. +Dumped_text = "===>" +arr+ "<=== (" +typeof(arr) + ")"; A } at returnDumped_text; -}
The next week to learn MongoDB,node learning is mainly through learning routines, read the source API to improve. After all, node has the advantage of a large network of NP.