Changes in the environment have brought about great technological leaps, bringing opportunities and challenges at the same time. Since I haven’t learned js yet, I had no choice but to put it on the shelf and learn it together. (>﹏<)
1. Read and read first
From the very beginning, I read "Introduction to Node.js in a Simple and Deep Language" without any hesitation, and I was confused. After reading it, I realized that this book requires a certain basic level. I searched the Internet and found the 41-page "Introduction to Node" and learned to build a Web application. And after refreshing my sense of accomplishment, I read "The Amazing Node.js". Now I am going to read "In-depth introduction to node.js" after reading this book.
2. Set up the environment and run the routine on git (error reported 3 times)
After downloading the msi from the official website, go all the way to next. When entering the command npm install, the error "npm ERR! not found: git" is reported. The reason is that the new computer does not follow git.
When installing external dependency packages, the error "gyp ERR! configure error" is reported. The reason is that the Python environment does not exist. And this dependency package can only be installed under lower versions. The last error reported was "MSBUILD: error MSB3428: Failed to load Visual C component "VCBuild.exe"". The error was still reported after installing .net framework 2.0 SDK and mvs2005. Decisively gave up.
3. The first web application
After running "Hello World", start writing a web application completely based on Node. First, you need to start the server module. The object returned by the createServer function in the http module has a listen([PORT]) method to listen to the port. The parameters of this function are very strange, it is a function definition. In JavaScript, a function can receive a parameter as another function. The parameter function is called an anonymous function. The reason for using this method is that node uses event-driven callbacks. When the server receives a request in a single process, it will call this function to handle the request. When a corresponding event occurs, the createServer function calls this function to call back.
After completing receiving the http request, we will process the request. First, we abstractly propose different URLs and GET and POST parameters. Two internal modules URL (parsing URL) and Query String (processing query string) are used here. And add the router module through dependency injection. Next, the module for processing requests needs to be completed to complete the routing. All requests are first passed between modules through an object (handle), and the handler can respond. Generally speaking, let the request handler directly return (return()) the information they want to display to the user through the onRequest function. However, it is very time-consuming to perform blocking operations, so we need to use non-blocking operations. Use callbacks by passing functions as arguments to other functions that take time to process. Instead of passing the content to the server, this time we "pass" the content from the server. That is to pass the response object (obtained from the server's callback function onRequest()) as a function parameter to the handler through routing. This way, the handler can call functions on the object.
Then proceed to the processing step, upload the image through the form, here the external dependency package formidable is used to receive the image. This module is used to process forms and upload files. First use var form = new formidable.IncomingForm(); to obtain a form object. The parse method of this object can parse and process form data. We simply save the file. The fs module is used to save files. Specify the save location through the rename method, and use the synchronization method renameSync() to make the file saving behavior precede the picture display behavior.
Displaying pictures is very simple. Read the file through the readFile method of fs, and use response.write(file, "binary") in its callback function to display the picture in the browser.
To summarize: Through this routine, we learned about the features in node.js, such as single-threaded, asynchronous, event-driven, etc. Some of these abstract concepts are also demonstrated through routines, but need to be understood in depth. Learning through routines means you need to look up information when you don't understand. This learning method is also a good Input-Person-Output method.