Premise: Before learning to develop an Internet site program, you need to know how a client request can be presented as a dazzling web page.
First, the Domain name request realization
This picture is enough to illustrate the process of a domain name request.
Second, server-side processing (nodejs example)
Directly on the NODEJS code
1 varHTTP = require (' http ');2 3Http.createserver (function(req, res) {4 if(Req.method = = = ' GET ') {5 varhtml;6 Switch(req.url) {7 Case‘/‘:8 Case'/index.html ':9html = ' Ten' <body> ' + One' A' <a href= ' add.html ' >add</a> ' + -' <a href= ' delete.html ' >delete</a> ' + -' <a href= ' update.html ' >update</a> ' + the' <a href= ' select.html ' >select</a> ' + -' </body>; - Break; - Case'/add.html ': +html = ' -' <body> ' + +' <a href= ' index.html ' >index</a> ' + A' Add ' + at' </body>; - Break; - Case'/delete.html ': -html = ' -' <body> ' + -' <a href= ' index.html ' >index</a> ' + in' Delete ' + -' </body>; to Break; + Case'/update.html ': -html = ' the' <body> ' + *' <a href= ' index.html ' >index</a> ' + $' Update ' +Panax Notoginseng' </body>; - Break; the Case'/select.html ': +html = ' A' <body> ' + the' <a href= ' index.html ' >index</a> ' + +' Delete ' + -' </body>; $ Break; $ default: -Res.writehead (' Content-type ', "text/html"); -Res.end (' 404 '); the return; - }WuyiRes.writehead (' Content-type ', "text/html"); the res.end (HTML); - } Wu}). Listen (7789); -Console.log (' http://localhost:7789 ');
View Code
Here you can see clearly
var http = require (' http ');//Create an HTTP object, what is HTTP? Baidu, Google go
Http.createserver (function (request, response) {}). Listen (7789)//Create a Httpserver server, and a listening port on the last side
Description
Premise: The server has used the node command to start the HTTP service and listen to Port 7798, this is relatively simple, save the above code as a file "Jswebsite.js", stored in the D disk,
At this point, from CMD into the D disk, run node jswebsite.js, create the HTTP service, and listen to the port to listen to.
Nodejs uses code to create HTTP protocol sites for servers, as well as listening ports, other Web servers are similar, IIS can create websites, and IP, port listening
1. When the request arrives the server, because the server already exists, corresponds to the request IP, the port listens, therefore will give the listener processing, here gives, when creates the HTTP service the anonymous function
Http.createserver (function (request, response) {}). Listen (7789), which is the function (Request,response) {} method processing.
2.request is mainly requested to carry information, response is mainly to respond to the expression of information
Request, including the requested method, URL, and many other information.
Response, including the response of head, Content-type and many other information
In fact, the other Web server, but also the response similar to encapsulate the above operations, this process is similar to the operation
No outside: request → processing → response
Extended:
1. You may see the above code switch case processing, very troublesome, how to do
Daniel has gone a lot and summed up the routing rules
2. Also see the trouble of handwritten HTML.
At this point, there will be corresponding to read the actual physical address file operation, the HTML code and logic separate, performance and logic separation,MVC, MVVM and so on
3. The overall structure of the website is a mess
At this time the corresponding website framework for your problems,Express and so on
Note: Nodejs Development Learning Catalogue