This week's nodejs learning content is about using several dependency packages, and I have knocked out all the examples in the book. This article uses routines as clues to review the work for a week.
1.connect
This routine mainly uses the connect dependency package, which provides a middleware (composed of functions and interacts with request and response objects). The book also introduces other middleware built-in to connect, which play the role of organizing code to complete web functions.
2.session
Use connect to perform user sessions and perform basic login system. The functions of checking login, displaying forms, user matching, and processing logout are realized through middleware. Shows the power and how middleware is organized.
3.express-tweet
This chapter learns to use the express package, express is based on connect. The programming is still simple, but in different versions of np, the function name will change, for example, express.createServer() becomes express() after 3.0. The biggest benefit of using Express is that it is simple and flexible. However, tweet cannot be accessed, and when rewritten as Weibo, weibo's API access was too complicated, and this program actually failed to run successfully.
4.echo
These two routines use the WebSocket package, and the main function of echo is to record the time of message transmission. The teasing method of Websocket is to listen to (on) an event, process it in the callback function, and also use js to interact with messages in the html file.
5.cursors
This example learns broadcasting, and this function is processed through a broadcast function written by itself. When ensuring that the cursor element exists, it is done through the DOM ID search.
6.chat
This chapter can be said to be the highlight of this book, because the Socket.IO package was developed by the author. Its difference with WS in terms of its messaging is based on transmission rather than entirely WS. In this routine, the chat function is first implemented, and then the broadcast song (DJ) function is implemented. There were no major problems encountered during the process. A process is socket.emit an event, and the socket on the other end listens to this event and processes it.
7. Summary
The efficiency this week is quite good, and the problems encountered are still on evernote. The problem has also appeared before, which is the difference between localhost and 127.0.0.1. This week, I learned a function during the debugging of JS, which can make JS dump an object like php. Now post it below to share.
The code copy 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 next week. The learning of node is mainly through learning routines and reading source code APIs . After all, the advantage of node is the np of blockbusters on the Internet.