Http module

  1. http module

    The http module is officially provided by Node.js and is used to create a web server module. Through the http.createServer() method, you can easily turn an ordinary computer into a Web server to provide Web resource services to the outside world.

    The difference between a server and an ordinary computer is that web server software such as IIS and Apache are installed on the server. In Node.js, we don’t need to use third-party web server software like IIS and Apache. Because you can use the http module to write server software and provide web services to the outside world.


  2. import http module

    1
    const http = require('http')


  1. Server related concepts
    1. IP address: It can be obtained by ping the domain name in the terminal. Enter 127.0.0.1 in the browser to access your computer as a server.

    2. Domain name and domain name server: The domain name (Domain Name) is a string corresponding to the IP address one-to-one, and this correspondence is stored in the domain name server (DNS, Domain name server).

    3. Port number: There may be multiple web services in one computer, each web service corresponds to a unique port number. The client’s network request will be handed over to the corresponding web service through the port number for processing. In practice, port 80 in url can be omitted.


  1. Create web server

    step

    1. Import the http module

      1
      const http = require('http')
    2. Create a web server instance

      You can quickly create a web server instance by calling the http.createServer() method.

      1
      const server = http.createServer()

    3. Bind the request event to the server to monitor client requests.

      Bind the request event to the server using the .on() method of the server instance. Whenever a client sends a request to the server, the request event is fired and the event handler is called.

      1
      2
      3
      server.on('request', (req,res)=>{
      console.log('client visit our web server.')
      })

    4. Start the server

      The current web server instance can be started by calling the .listen() method of the server instance. The first parameter is the port number, and parameter 2 (callback function) will be called after the startup is successful.

      1
      2
      3
      server.listen(80, ()=>{
      console.log('http server running at http://127.0.0.1')
      })

      Stop the server with CTRL+C. Same as scaffolding.

      After the server modifies the code, the server must be restarted for the code to take effect.


  1. req request object

    As long as the server receives the client’s request, it will call the server.on() bound request event handler for the server.

    If you want to access client-related data or properties in the event handler, you can use the req request object:

    req.url : The url address requested by the client (not the client address, but the target address you want to request. The address is not the complete address, but starts from the part after the port number.)

    req.method : the client’s method request type (GET, POST, etc.)


  2. res response object

    In the server’s request event handler, If you want to access server-related data or properties, you can use the res response object. For example, after the client accesses the server, if it wants to display the server content on the client’s web page, it needs to use the res response object.

    The res response object is the second parameter in the callback function. If you want to use res instead of req, you also need to complete req.

    The res.end() parameter is a string, which is sent to the client.


  3. Solve the problem of Chinese garbled characters

    When calling the res.end() method to send Chinese content to the client, there will be garbled characters. At this time, you need to manually set the encoding format.

    1
    res.setHeader('Content-Type', 'text/html; charset=utf-8')

    Both parameters are fixed notation.


  4. Respond to different content according to different urls

    step:

    1. Get the url address of the request

    2. Set the default corresponding content to 404 Not found

    3. Determine whether the user request is / or /index.html home page

    4. Determine whether the user request is the /about.html page

    5. (If Chinese is involved,) set the Content-Type response header to prevent Chinese garbled characters

    6. Response content to client using res.end()


    Code:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    const http = require('http')
    const server = http.createServer()

    server.on('request', function(req, res){
    const url = req.url
    let content = '<h1> 404 Not Found! </h1>'
    if (url === '/' || url === '/index.html'){
    content = '<h1> main page <h1>'
    }else if (url === '/about.html'){
    content = '<h1> about page <h1>'
    }
    res.setHeader('Content-type', 'text/html; charset=utf-8')
    res.end(content)
    })

    server.listen(80, ()=>{
    console.log('server running at http://127.0.0.1')
    })

Share