Read the HTML file and render it to the page through the nodejs server

1. Simple implementation of three alternate pages respectively.

 

  • login.html page
  • index.html page
  • code segment:
 
  1. <!DOCTYPE html>  
  2. <html>  
  3. <head>  
  4. <meta charset="utf-8">   
  5. < title >Rookie Tutorial (runoob.com) </ title >   
  6. <style>  
  7. .center {  
  8.     margin: auto;  
  9.     width: 60%;  
  10.     border: 3px solid #73AD21;  
  11.     padding: 10px;  
  12. }  
  13. </style>  
  14. </head>  
  15. <body>  
  16.   
  17. < h2 >Elements are centered </ h2 >  
  18. < p > To center block-level elements (such as div ) horizontally, you can use margin: auto; </ p >  
  19.   
  20. <div class="center">  
  21.   < p > < b >Note:  </ b >Using margin:auto is not compatible with IE8 unless !DOCTYPE is declared. </ p >  
  22. </div>  
  23.   
  24. </body>  
  25. </html>  

 

  • notFount.html page
  1. <!DOCTYPE HTML>  
  2. <html>  
  3. <head>  
  4. <script>  
  5.   
  6. </script>  
  7. <style>  
  8. .center {  
  9.     margin: auto;  
  10.     width: 60%;  
  11.     border: 3px solid #73AD21;  
  12.     padding: 10px;  
  13.     color:red;  
  14. }  
  15. </style>  
  16. </head>  
  17. <body>  
  18.   
  19. <div class ="center" >404 Not Fount</div>  
  20.   
  21. </body>  
  22. </html>  

2. Modify the created nodejs server page and make different response pages for requests from different addresses.

 

  • Add the file reading code in the url address judgment to realize the reading of the defined html page.
  • Declare the filesystem object:
  1. // Declare the file OS object  
  2.     var fs = require('fs');  
  • Implement file content reading and rendering to the page
 
  1. if(url ==='/'){  
  2.     //response.writeHead(response status code, response header object): Send a response header to the request.  
  3.     response.writeHead(200,{'Content-Type':'text/html'})  
  4.     // If url='/', read the html file under the specified file and render it to the page.  
  5.     fs.readFile('./practice/login.html','utf-8',function(err,data){  
  6.     if(err){  
  7.     throw err ;  
  8.     }  
  9.     response.end(data);  
  10.     });  
  11. }  

 

  • Full code:
 
  1. /**  
  2.       
  3. 1. To interact with the client using the HTTP server, require('http') is required.  
  4.     declare http protocol  
  5. */  
  6. var http = require('http');  
  7.   
  8.   
  9. // Declare the file OS object  
  10. var fs = require('fs');  
  11. /**  
  12. 2. Get the server object  
  13.     1. Create a service through http.createServer([requestListener])  
  14.   
  15.     requestListener <Function>  
  16.     Returns:  < http.Server >  
  17.     Returns a newly created http.Server instance.  
  18.     For the server, there are three main things to do:  
  19.     1. Accept the request from the client.  
  20.     2. Process the request sent by the client.  
  21.     3. Send a response to the client.  
  22. */  
  23.   
  24. var server = http.createServer();  
  25.   
  26. /**  
  27. 3. Declare the port number and start the service.  
  28.     server.listen([port][, host][, backlog][, callback])  
  29.   
  30.     port  < number > : port number  
  31.     host  < string > : host ip  
  32.     Generic parameters for the backlog  < number > server.listen() function  
  33.     callback  < Function > Generic parameter for server.listen() function  
  34.     Returns: <net.Server>  
  35.     Start a TCP service listening on the input port and host.  
  36.   
  37.     If port is omitted or 0, the system will arbitrarily assign an unused port that can be retrieved by server.address().port after the 'listening' event fires.  
  38.   
  39.     If host is omitted, the server will accept connections based on the unspecified IPv6 address (::) if IPv6 is available, otherwise it will accept connections based on the unspecified IPv4 address (0.0.0.0)  
  40.   
  41. */  
  42. server.listen(9001, function(){  
  43.      console.log('The server is running on port number: 9001...');  
  44. })  
  45.   
  46.   
  47. /**  
  48. 4. Add a request request event to the server instance object, which is the entry point for all requests.  
  49.     Any request will trigger the change event, and then execute the handler function corresponding to the event.  
  50.   
  51.     server.on('request',function(){  
  52.          console.log('Received a request from the client......');  
  53.     });  
  54. */  
  55.   
  56.   
  57. /**  
  58. 5. Set the request processing function.  
  59.     The request callback handler needs to receive two parameters.  
  60.     request : request is a request object that can get some information about the current browser request.  
  61.         eg: request path, request method, etc.  
  62.     response: response is a response object that can be used to send a response to the request.  
  63.   
  64. */  
  65. server.on('request',function(request,response){  
  66.       
  67.     var url = request.url;  
  68.     if(url ==='/'){  
  69.         //response.writeHead(response status code, response header object): Send a response header to the request.  
  70.         response.writeHead(200,{'Content-Type':'text/html'})  
  71.         // If url='/', read the html file under the specified file and render it to the page.  
  72.         fs.readFile('./practice/login.html','utf-8',function(err,data){  
  73.             if(err){  
  74.                 throw err ;  
  75.             }  
  76.             response.end(data);  
  77.         });  
  78.       
  79.     }else if(url === '/login'){  
  80.         response.writeHead(200,{'Content-Type':'text/html'});  
  81.         // If url='/', read the html file under the specified file and render it to the page.  
  82.         fs.readFile('./practice/login.html','utf-8',function(err,data){  
  83.             if(err){  
  84.                 throw err ;  
  85.             }  
  86.             response.end(data);  
  87.         });  
  88.     }else if(url === '/index'){  
  89.         response.writeHead(200,{'Content-Type':'text/html'});  
  90.         // If url='/', read the html file under the specified file and render it to the page.  
  91.         fs.readFile('./practice/index.html','utf-8',function(err,data){  
  92.             if(err){  
  93.                 throw err ;  
  94.             }  
  95.             response.end(data);  
  96.         });  
  97.     }else{  
  98.         response.writeHead(200,{'Content-Type':'text/html'});  
  99.         // If url='/', read the html file under the specified file and render it to the page.  
  100.         fs.readFile('./practice/notFount.html','utf-8',function(err,data){  
  101.             if(err){  
  102.                 throw err ;  
  103.             }  
  104.             response.end(data);  
  105.         });  
  106.     }  
  107.       
  108. });  
  • The final effect:
  • Start the nodejs server and enter in the address bar: 127.0.0.0.1:9001 or 127.0.0.0.1:9001/login

 

  • In the address bar enter: 127.0.0.0.1:9001/index

 

  • In the address bar enter: 127.0.0.0.1:9001/something else



 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324990856&siteId=291194637