SoFunction
Updated on 2025-03-10

Detailed explanation of service instances that use Linux to create access to static web pages

Detailed explanation of service instances that use Linux to create access to static web pages

1. The environment required for installation and operation:https:///article/

2. Create a node directory (/node/www) and create a service file in the directory

var http = require('http');

var fs = require('fs');//Introduce file reading module
var documentRoot = ‘/node/www';//Storage directory of files that need to be accessed
var server= (function(req,res){

  var url = ; 
  //The URL entered by the client, for example, if you enter localhost:9999/  //Then the url here == /
  var file = documentRoot + url;
  (url);//node/www/ 
  
  /*
     file is file path
     function is a callback function,
     The err of function reads the information returned by the error. If the return is empty, there will be no error.
     The data of function is the text content returned by the successful reading
   */
  ( file , function(err,data){
    if(err){
      (404,{
        'content-type' : 'text/html;charset="utf-8"'
      });
      ('<h1>404 error</h1><p>The page you are looking for does not exist</p>');
      ();
    }else{
      (200,{
        'content-type' : 'text/html;charset="utf-8"'
      });
      (data);// will be displayed on the client      ();
    }

  });

}).listen(9999);

('The server is enabled successfully...');

3. Create the homepage file and place it under the path as /node/www/

4. Start the service command: node

5. Browser input address: http://localhost:9999/

Thank you for reading, I hope it can help you. Thank you for your support for this site!