A great routine analysis of node.js's reading notes

  • 2020-05-09 18:05:47
  • OfStack

This week's nodejs is about the use of several dependent packages. I typed all the examples in the book once. This article will take the routine as the clue, review 1 next week's work.

1.connect

The routine   mainly USES the connect dependency package. connect provides a middleware (composed of functions that interact with the request and response objects). The book also introduces other middleware built into connect that organizes the code to accomplish web's functions.

2.session

  USES connect for user sessions and basic login to the system. The functions of checking login, displaying form, user matching and processing logout are realized through middleware. Demonstrates the power and organization of middleware.

3.express-tweet

In this chapter, you learned to use the express package, and express is based on connect. The programming is still simple, but in different versions of np, the name of the function will change a little. For example, express.createServer () will become express() after 3.0. The biggest benefit of using express is its simplicity and flexibility. However, tweet is not accessible, and when rewritten as a tweet, weibo's API access is so complex that the program actually failed to run.

4.echo

The two routines   use the WebSocket package, and echo's main function is to record the time of message transfer. The Websocket way of flirting is to listen for (on) 1 event on ws, to be processed in the callback function, and also to use js in the html file for message interaction.

5.cursors

In this example,   learned about broadcasting, which is handled by a self-written broadcast function. Making sure the cursor element is present is done by ID lookup on DOM.

6.chat

This chapter is the highlight of the book, because the Socket.IO package was developed by the author. It differs from WS in that its messaging is based on transport and is not all WS. This routine implements the chat function first, then the broadcast song (DJ) function. There were no major problems. One process is the socket.emit1 event, and the other end, socket, listens to this event and processes it.

7. To summarize

The efficiency of   is quite good this week. The problems encountered are still on evernote. The problem has been the difference between localhost and 127.0.0.1 before. During the debugging of js this week, I learned a function, which enables js to create dump1 objects like php1. Now post to share below.


 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 to learn mongoDB in the next week. The learning of node is mainly improved by learning routines and reading the source code API. After all, the good thing about node is that it's a blockbuster on the web.


Related articles: