Common HTTP protocol status codes and their explanations
Dec 26, 2023 pm 03:07 PMTo 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.
-
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'); });
Copy after login 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'); });
Copy after login500 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'); });
Copy after login302 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'); });
Copy after login
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!

Hot Article

Hot tools Tags

Hot Article

Hot Article Tags

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

Introduction to HTTP 525 status code: explore its definition and application

How to turn off the content display function of Kuaishou private messages? What does it mean to turn off the display content of Kuaishou private messages?

Understand common application scenarios of web page redirection and understand the HTTP 301 status code

Detailed explanation of the meaning of MySQL host name

How to implement HTTP streaming using C++?
