Node.js is a simple HTTP server that can be used to manipulate computer resources.

What is an HTTP service?

What is the HTTP protocol?

  • Hypertext Transfer Protocol (HYPERtext Transfer Protocol), an application-layer protocol, is a protocol and specification for the transfer of hypertext data, such as text, pictures, audio and video, between two points in the computer world.

A web page request that contains two HTTP packet exchanges:

  • The browser sends a request HTTP packet to the HTTP server
  • The HTTP server returns HTTP packets to the browser

What does an HTTP service do?

  • The HTTP request packet is parsed
  • Returns the corresponding HTTP return packet

Implement a simple HTTP server

Create a new http.js file and write the following code:

// HTTP is a Node package loaded here
const http = require('http')

// Create a Web static server with http.createserver
http.createServer(function (request, response) {
  // What happens after the request is heard
  // The request object contains all contents of the user request message
  // We can use the request object to get the data submitted by the user

  // response Response object, used to respond to some data
  // When the server wants to respond to data from the client, it must use a Response object
  response.writeHead(200);
  response.end('hello world');
}).listen(4000.function () {
  // Start the service by listening to the port
  console.log("The server is started and can be accessed at http://localhost:4000.");
})
Copy the code

The terminal runs the node http.js command

As you can see, the service has started. Open http://localhost:4000 in Chrome:

Response.end () is displayed on the page, and a simple HTTP server is implemented.

fsThe module loads static resources

Create a new index.js file:

// Load the module
const http = require('http')
const fs = require('fs');

// Create a service
http.createServer(function (request, response) {
  console.log(request.url);
  response.writeHead(200);
  response.end();
}).listen(3000)
Copy the code

Run: node index.js on the terminal and open localhost:3000 in the browser

Two requests are sent, one for the current URL http://localhost:3000/; Another is the upper right corner of the icon http://localhost:3000/favicon.ico request:

/favicon.ico /favicon.ico /favicon.ico /favicon.ico /favicon.ico /favicon.ico /favicon.ico Then use the FS module to handle static resources:

// Load the module
const http = require('http')
const fs = require('fs');

// Create a service
http.createServer(function (request, response) {
  // console.log(request.url);
  // Return 200 if icon request is made
  if (request.url == '/favicon.ico') {
    response.writeHead(200);
    response.end()
    return
  }

  response.writeHead(200);
  // fs is a file module that can read local files via createReadStream
  // Write the response object through pipe
  fs.createReadStream(__dirname + '/index.html').pipe(response)
}).listen(3000)
Copy the code

The contents of the index.html file are as follows:

<! DOCTYPEhtml>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="Width = device - width, initial - scale = 1.0">
  <title>Implement a simple HTTP server</title>
</head>
<body>
  <div>Hello HTTP service</div>
</body>
</html>
Copy the code

Terminal running: node index.js Start service:

As you can see, the HTTP server has already sent the computer’s static resource index.html to the browser.

Such a simple read computer static resources HTTP server is realized!

The HTTP server uses the two built-in Node.js modules HTTP and FS, and there are many other modules in Node.js that help us achieve powerful functions and make the Node.js ecosystem even more powerful.

Here’s the code