forked from faisal2410/terminology
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path8_http_response_status_code.js
More file actions
44 lines (25 loc) · 2.73 KB
/
Copy path8_http_response_status_code.js
File metadata and controls
44 lines (25 loc) · 2.73 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
/*
HTTP response status codes are three-digit numbers that are sent back from the server to the client to indicate the outcome of an HTTP request. They are an important part of the HTTP protocol and are used to provide information about the status of the request and any errors that may have occurred.
Here are some common HTTP status codes that you may encounter in the MERN stack, along with their meanings:
200 OK: This status code indicates that the request was successful and the server has returned the requested data. For example, when a GET request is made to retrieve a user's data, the server might respond with a 200 OK status code and the user's information.
201 Created: This status code indicates that the request was successful and a new resource was created. For example, when a POST request is made to create a new user, the server might respond with a 201 Created status code and the newly created user's information.
400 Bad Request: This status code indicates that the request was malformed or invalid. For example, if a required parameter is missing from a POST request, the server might respond with a 400 Bad Request status code.
401 Unauthorized: This status code indicates that the user making the request is not authorized to access the requested resource. For example, if a user tries to access another user's data without the proper permissions, the server might respond with a 401 Unauthorized status code.
404 Not Found: This status code indicates that the requested resource was not found on the server. For example, if a user tries to access a URL that doesn't exist, the server might respond with a 404 Not Found status code.
500 Internal Server Error: This status code indicates that an error occurred on the server while processing the request. For example, if there is a problem with the server's database connection, the server might respond with a 500 Internal Server Error status code.
In the MERN stack, you can set the status code of an HTTP response using the status() function provided by the Express.js framework. Here's an example:
javascript
Copy code
app.get('/users/:id', (req, res) => {
const userId = req.params.id;
// Get user data from the database
const userData = getUserById(userId);
if (userData) {
res.status(200).json(userData);
} else {
res.status(404).json({ message: `User with ID ${userId} not found` });
}
});
In this example, we're defining a route that retrieves a user's data by their ID. If the user is found, we send back a 200 OK status code along with the user's data in JSON format. If the user is not found, we send back a 404 Not Found status code along with a JSON object containing an error message.
I hope this helps you understand HTTP response status codes in the MERN stack!
*/