For front-end development, Web applications are not unfamiliar to us. To discuss today, we need some basic knowledge reserves when developing a Web application. We know that a Web application can't get rid of (request) requests and responses (response), and everything we want to do is out of the way. So what exactly do we need to focus on? In the Nodejs, how to realize and use it? Let's look at the details below. 1. Request method Use the browser to access any Web site, grab the packet to view the network request, will be found in the request header message in this line
get/http/1.1//access to http://www.so.com results //The first is the request method; The second is the access path; the third is the HTTP version number
The first value is the request method we're going to say here, and we can respond to the user's request by listening to the link to the node service.
Function (req, res) { //use Req.method to receive different types of requests and do the corresponding processing //method values are: GET POST PUT CONNECT DELETE }
Above, you can distribute the business logic according to the request method. 2. Path resolution in the request method, the value of the second part of the first line of the request message is the path to the request. When we receive this path request, the node server responds to the request in two ways. The first is a static file that can read the contents of the file directly through the path and then give a response. It is important to note that the URL module can handle URL-related operations in Nodejs.
var url = require (' URL '); var fs = require (' FS '); Function (req, res) { var pathname = Url.parse (req.url). Pathname; Fs.readfile (pathname, function (err, file) { if (err) { //returns 404 return ; } Res.end (file); Normal return }); }
The second, is to implement a routing rule, when access to an address, according to the defined routing rules read different resources, and then give the corresponding response. 3. Query character channeling or in the second part of the first line of the request message, in addition to the path, there will be some parameters we need to pass-that is, the query string. The same node provides the QueryString module to handle this part of the data.
var url = require (' URL '); var querystring = require (' querystring '); Function (req, res) { var query = Querystring.parse (Url.parse (Req.url)). query; Note the key in the query string appears more than once, and the result is an array }
4.cookie and Session cookie settings There are two cases, the first through the server Set-cookie to the browser to set cookies, the second is browser script (JavaScript), the final effect of the two settings is the same. Basic use of cookies I will not do too much to explain, the main attention to the points:
- Every time the browser sends a cookie to the server
- If the cookie is set http-only, the front end cannot be obtained by document.cookie
- When the cookie is set to true, it can only be transmitted over HTTPS
- Cookie security issues to prevent XSS and csrf issues from appearing
The cookie itself has no size limit, and all his restrictions are from the browser and server configuration. Of course, a reasonable use of cookies to the performance and security of the page is guaranteed. Session is a scheme to address cookie size and security, which is generated by the server and exists in memory. He can also use some encryption algorithms to make the session more secure for transmission. In order to solve the session sharing problem, we usually use third-party tools such as Memacha, Redis, etc. to manage it. In Nodejs, the session function is not provided and can be used using the session module. 5. Cached in the B/S mode of the page, in many cases will not be updated frequently, this time if every visit to request, on the one hand on the resources is a waste, on the other hand may take a long time, the random cache technology came into being ~ in order to improve performance, YSlow has so few cache rules:
There are problems: first of all, the time itself is error, and then maybe my modification time has changed, but my content has not changed, my intention is only to re-request when the content changes, this way, will add a part of the request.
Solve the problem: solve the problem of expiration time, this value is set by the server, in Nodejs, you need to implement. The principle is that a file change is detected to adjust this value, whereas the other is unchanged.
Problem: Confirm that the request does not change the data to do the cache, if the data real-time requirements are relatively high, it can not be used, so use must be cautious. In summary, before we used the Apache, Nginx to help us deal with a lot, we do not need to care about the configuration. And in Nodejs, these all need to be realized by themselves. Reference: "Nodejs"-building Web applications
What do we need to know about Web applications?