1. Simply implement three alternate pages respectively.
- Login.html page
- Index.html page
- Code snippet:
- <! DOCTYPE HTML>
- <html>
- <head>
- <Meta charset="Utf-8">
- <title> Rookie Tutorial (runoob.com)</title>
- <style>
- . Center {
- Margin:auto;
- width:60%;
- BORDER:3PX solid #73AD21;
- padding:10px;
- }
- </style>
- </head>
- <body>
- <h2> element Center align </h2>
- <p> Horizontal center block-level elements (such as DIV), you can use Margin:auto; </P>
- <div class="center">
- <p><b> Note: </b> Using Margin:auto cannot be compatible with IE8 unless! DOCTYPE has declared. </P>
- </div>
- </body>
- </html>
- <! DOCTYPE HTML>
- <html>
- <head>
- <script>
- </Script>
- <style>
- . Center {
- Margin:auto;
- width:60%;
- BORDER:3PX solid #73AD21;
- padding:10px;
- color:red;
- }
- </style>
- </head>
- <body>
- <div class ="center" >404 not fount</div>
- </body>
- </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:
- Declaring file operating system objects
- var fs = require (' FS ');
- Implement file content read and render to page
- if (url = = = '/') {
- Response.writehead (response status Code, Response header Object): Sends a response header to the request.
- Response.writehead (200,{' content-type ': ' text/html ')
- If url= '/', read the HTML file under the specified file and render to the page.
- Fs.readfile ('./practice/login.html ', ' utf-8 ', function (err,data) {
- if (err) {
- throw err;
- }
- Response.End (data);
- });
- }
- /**
- 1. Using an HTTP server to interact with the client requires require (' HTTP ').
- Declaring the HTTP protocol
- */
- var http = require (' http ');
- Declaring file operating system objects
- var fs = require (' FS ');
- /**
- 2. Get the server object
- 1. Create a service from Http.createserver ([Requestlistener])
- Requestlistener <Function>
- Return: <http. Server>
- Returns a new HTTP. Server instance.
- For the service side, the main do three things:
- 1. Accept requests made by the client.
- 2. Handle requests from the client.
- 3. Send a response to the client.
- */
- var server = http.createserver ();
- /**
- 3. Declare the port number and turn on the service.
- Server.listen ([port][, host][, backlog][, callback])
- Port <number >: Port numbers
- Host <string>: Hosts IP
- General parameters for the backlog < number > Server.listen () function
- General parameters for callback < function > Server.listen () functions
- Returns: <net. Server>
- Start a TCP service to listen for the input port and host.
- 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.
- 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)
- */
- Server.listen (9001, function () {
- Console.log (' server is running on port number: 9001 ... ');
- })
- /**
- 4. Add the request requests event to the server instance object, which is the entry for all requests.
- Any request triggers a change event and then executes the handler function corresponding to the event.
- Server.on (' request ', function () {
- Console.log (' received a request from the client ... ');
- });
- */
- /**
- 5. Set the request handler function.
- The request callback handler needs to receive two parameters.
- Request:request is a request object that can get some information from the current browser request.
- Eg: request path, request method, etc.
- Response:response is a response object that can be used to send a response to a request.
- */
- Server.on (' request ', function (request,response) {
- var url = request.url;
- if (url = = = '/') {
- Response.writehead (response status Code, Response header Object): Sends a response header to the request.
- Response.writehead (200,{' content-type ': ' text/html ')
- If url= '/', read the HTML file under the specified file and render to the page.
- Fs.readfile ('./practice/login.html ', ' utf-8 ', function (err,data) {
- if (err) {
- throw err;
- }
- Response.End (data);
- });
- }else if (url = = = '/login ') {
- Response.writehead (200,{' content-type ': ' text/html ');
- If url= '/', read the HTML file under the specified file and render to the page.
- Fs.readfile ('./practice/login.html ', ' utf-8 ', function (err,data) {
- if (err) {
- throw err;
- }
- Response.End (data);
- });
- }else if (url = = = '/index ') {
- Response.writehead (200,{' content-type ': ' text/html ');
- If url= '/', read the HTML file under the specified file and render to the page.
- Fs.readfile ('./practice/index.html ', ' utf-8 ', function (err,data) {
- if (err) {
- throw err;
- }
- Response.End (data);
- });
- }else{
- Response.writehead (200,{' content-type ': ' text/html ');
- If url= '/', read the HTML file under the specified file and render to the page.
- Fs.readfile ('./practice/notfount.html ', ' utf-8 ', function (err,data) {
- if (err) {
- throw err;
- }
- Response.End (data);
- });
- }
- });
- 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