Read HTML file rendering to page via Nodejs server

Source: Internet
Author: User
Tags readfile

1. Simply implement three alternate pages respectively.

    • Login.html page
    • Index.html page
    • Code snippet:
  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. <h2> element Center align </h2>
  17. <p> Horizontal center block-level elements (such as DIV), you can use Margin:auto; </P>
  18. <div class="center">
  19. <p><b> Note: </b> Using Margin:auto cannot be compatible with IE8 unless! DOCTYPE has declared. </P>
  20. </div>
  21. </body>
  22. </html>

    • Notfount.html page
  1. <! DOCTYPE HTML>
  2. <html>
  3. <head>
  4. <script>
  5. </Script>
  6. <style>
  7. . Center {
  8. Margin:auto;
  9. width:60%;
  10. BORDER:3PX solid #73AD21;
  11. padding:10px;
  12. color:red;
  13. }
  14. </style>
  15. </head>
  16. <body>
  17. <div class ="center" >404 not fount</div>
  18. </body>
  19. </html>

2. Modify the Created Nodejs Server page to make different response pages for requests from different addresses.

    • In URL address judgment, add the file to read the code to implement the read definition of the HTML page.
    • Declaring File system objects:
    1. Declaring file operating system objects
    2. var fs = require (' FS ');
    • Implement file content read and render to page
    1. if (url = = = '/') {
    2. Response.writehead (response status Code, Response header Object): Sends 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 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. 1. Using an HTTP server to interact with the client requires require (' HTTP ').
  3. Declaring the HTTP protocol
  4. */
  5. var http = require (' http ');
  6. Declaring file operating system objects
  7. var fs = require (' FS ');
  8. /**
  9. 2. Get the server object
  10. 1. Create a service from Http.createserver ([Requestlistener])
  11. Requestlistener <Function>
  12. Return: <http. Server>
  13. Returns a new HTTP. Server instance.
  14. For the service side, the main do three things:
  15. 1. Accept requests made by the client.
  16. 2. Handle requests from the client.
  17. 3. Send a response to the client.
  18. */
  19. var server = http.createserver ();
  20. /**
  21. 3. Declare the port number and turn on the service.
  22. Server.listen ([port][, host][, backlog][, callback])
  23. Port <number >: Port numbers
  24. Host <string>: Hosts IP
  25. General parameters for the backlog < number > Server.listen () function
  26. General parameters for callback < function > Server.listen () functions
  27. Returns: <net. Server>
  28. Start a TCP service to listen for the input port and host.
  29. If port is omitted or 0, the system is arbitrarily assigned a useless port that can be retrieved by server.address () after the ' listening ' event is triggered.
  30. If host is omitted, if IPV6 is available, the server receives a connection based on the unspecified IPV6 address (::), otherwise receives a connection based on unspecified IPv4 address (0.0.0.0)
  31. */
  32. Server.listen (9001, function () {
  33. Console.log (' server is running on port number: 9001 ... ');
  34. })
  35. /**
  36. 4. Add the request requests event to the server instance object, which is the entry for all requests.
  37. Any request triggers a change event and then executes the handler function corresponding to the event.
  38. Server.on (' request ', function () {
  39. Console.log (' received a request from the client ... ');
  40. });
  41. */
  42. /**
  43. 5. Set the request handler function.
  44. The request callback handler needs to receive two parameters.
  45. Request:request is a request object that can get some information from the current browser request.
  46. Eg: request path, request method, etc.
  47. Response:response is a response object that can be used to send a response to a request.
  48. */
  49. Server.on (' request ', function (request,response) {
  50. var url = request.url;
  51. if (url = = = '/') {
  52. Response.writehead (response status Code, Response header Object): Sends a response header to the request.
  53. Response.writehead (200,{' content-type ': ' text/html ')
  54. If url= '/', read the HTML file under the specified file and render to the page.
  55. Fs.readfile ('./practice/login.html ', ' utf-8 ', function (err,data) {
  56. if (err) {
  57. throw err;
  58. }
  59. Response.End (data);
  60. });
  61. }else if (url = = = '/login ') {
  62. Response.writehead (200,{' content-type ': ' text/html ');
  63. If url= '/', read the HTML file under the specified file and render to the page.
  64. Fs.readfile ('./practice/login.html ', ' utf-8 ', function (err,data) {
  65. if (err) {
  66. throw err;
  67. }
  68. Response.End (data);
  69. });
  70. }else if (url = = = '/index ') {
  71. Response.writehead (200,{' content-type ': ' text/html ');
  72. If url= '/', read the HTML file under the specified file and render to the page.
  73. Fs.readfile ('./practice/index.html ', ' utf-8 ', function (err,data) {
  74. if (err) {
  75. throw err;
  76. }
  77. Response.End (data);
  78. });
  79. }else{
  80. Response.writehead (200,{' content-type ': ' text/html ');
  81. If url= '/', read the HTML file under the specified file and render to the page.
  82. Fs.readfile ('./practice/notfount.html ', ' utf-8 ', function (err,data) {
  83. if (err) {
  84. throw err;
  85. }
  86. Response.End (data);
  87. });
  88. }
  89. });
    • Final results:
    • Open the Nodejs server and enter it in the Address bar: 127.0.0.0.1:9001 or 127.0.0.0.1:9001/login

    • In the Address bar, type: 127.0.0.0.1:9001/index

    • Enter in the Address bar: 127.0.0.0.1:9001/other content



Read HTML file rendering to page via Nodejs server

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.