Hướng dẫn thử thách
1 / 4
What are HTTP Response Status Codes?
Whenever you visit a website or use an API, your browser and the server are constantly talking to each other.
One of the most important parts of that conversation? HTTP response status codes.
They're short numeric messages — like 200, 404, or 500 — that tell the client (the end user's device such as a laptop or phone) what happened after a request was made.
You've learned some of these already, but let's break them down by category so you can recall exactly what they mean and when to use them.
## 1xx – Informational Responses
Codes that start with 1 are rarely seen in everyday development, but they indicate that the request was received and the process is continuing like when uploading a file.
Common example:
* 100 Continue – The server tells the client, "Okay, you can send the rest of the request."
* 101 Switching Protocols – The client sent an `Upgrade` header so the server is switching protocols. You'll see this most commonly when using Websockets.
## 2xx – Success Responses
These mean the request was successfully received, understood, and processed.
* 200 OK – The request succeeded. This is the most common success response.
* 201 Created – A new resource was successfully created, for example, after registering a new user.
* 204 No Content – The request succeeded, but there's no response body to send back. Often used for `DELETE` requests.
## 3xx – Redirection Messages
These tell the client: "The resource you're looking for is somewhere else."
* 301 Moved Permanently – The resource has a new permanent URL.
* 302 Found – The resource is temporarily at a different URL.
* 304 Not Modified – Used for caching. It tells the client nothing has changed, so it can use the cached version.
## 4xx – Client Errors
These happen when the client sends an invalid request.
* 400 Bad Request – The request is malformed or invalid.
* 401 Unauthorized – Authentication is required (you need to log in).
* 403 Forbidden – Authentication succeeded, but you don't have permission.
* 404 Not Found – The requested resource does not exist. And yes, this is the most iconic error on the web.
## 5xx – Server Errors
These mean something went wrong on the server side.
* 500 Internal Server Error – A generic "something went wrong" message.
* 502 Bad Gateway – A server acting as a proxy received an invalid response from another server.
* 503 Service Unavailable – The server is temporarily overloaded or down for maintenance.
So now that you're familiar with some of the most common HTTP status codes, let's look at some practical examples in Express.
Let's say you're building an API. If a user sends invalid data, you can respond with a 400 status code, which means "Bad Request". You can also send the string "Bad Request" back to the client so it's clear what the 400 response code means:
```js
res.status(400).send("Bad Request");
```
If everything goes well, you can send a 200 response. You can include a message if you'd like. Here we're sending some JSON with a simple "Success" message:
```js
res.status(200).json({ message: "Success" });
```
And if something crashes on the server, you can send a 500 status code which means an internal server error occurred, along with a clear message about the error:
```js
res.status(500).send("Internal Server Error");
```
These status codes aren't just for machines. They help developers debug issues and help users (and other developers) understand what's going on.
Vượt qua bài kiểm tra hiện tại để mở khóa bài tiếp theo.
main.sh
UTF-8 • Tab Size: 2Kiểm tra bài:⌘↵