Home >Web Front-end >HTML Tutorial >Common HTTP protocol status codes and their explanations

Common HTTP protocol status codes and their explanations

王林
王林Original
2023-12-26 15:07:161177browse

Common HTTP protocol status codes and their explanations

To understand common HTTP protocol status codes and their meanings, specific code examples are needed

HTTP protocol is one of the most important application layer protocols in modern network communications. In the process of web development, we often encounter various HTTP status codes. This article will detail some common HTTP status codes and their meanings, and provide corresponding code examples.

  1. 200 OK
    200 OK is one of the most common HTTP status codes, indicating that the request was successful and the requested resource was returned. Usually, after the client sends a GET request, the server will return the status code and corresponding content.

    Code example:

    const http = require('http');
    const server = http.createServer((req, res) => {
      res.statusCode = 200;
      res.setHeader('Content-Type', 'text/plain');
      res.end('Hello, world!');
    });
    
    server.listen(3000, 'localhost', () => {
      console.log('Server started on port 3000');
    });
  2. 404 Not Found
    404 Not Found means that the resource requested by the client does not exist. This status code is returned when the server cannot find the requested resource.

    Code example:

    const http = require('http');
    const server = http.createServer((req, res) => {
      res.statusCode = 404;
      res.setHeader('Content-Type', 'text/plain');
      res.end('404 - Not Found');
    });
    
    server.listen(3000, 'localhost', () => {
      console.log('Server started on port 3000');
    });
  3. 500 Internal Server Error
    500 Internal Server Error indicates that an unknown error occurred on the server and the client's request cannot be completed. This is usually caused by a bug in the server's internal programming.

    Code example:

    const http = require('http');
    const server = http.createServer((req, res) => {
      res.statusCode = 500;
      res.setHeader('Content-Type', 'text/plain');
      res.end('500 - Internal Server Error');
    });
    
    server.listen(3000, 'localhost', () => {
      console.log('Server started on port 3000');
    });
  4. 302 Found
    302 Found indicates that the requested resource has been temporarily moved to another URL. The server will return the new URL in the response header, and the client can resend the request based on this URL.

    Code sample:

    const http = require('http');
    const server = http.createServer((req, res) => {
      res.statusCode = 302;
      res.setHeader('Location', 'https://www.example.com/new-url');
      res.end();
    });
    
    server.listen(3000, 'localhost', () => {
      console.log('Server started on port 3000');
    });

The above are just some of the common HTTP status codes and their meanings. There are many other status codes in the HTTP protocol. During development, understanding and correctly handling different status codes is crucial to developing efficient web applications. We hope that the code examples provided in this article can help readers better understand the meaning of each status code.

The above is the detailed content of Common HTTP protocol status codes and their explanations. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn