The Status Line: Your First Indication
Think of the status line as the server's immediate reaction to your request. Just as a waiter might first say "Coming right up!" or "I'm sorry, we're out of that," the status line gives you an instant understanding of how your request was received.
Example: Working with Status Lines
class HTTPResponse {
constructor(version = 'HTTP/1.1') {
this.version = version;
this.statusCode = 200;
this.statusText = 'OK';
this.headers = new Map();
this.body = null;
}
setStatus(code, text = null) {
// Let's understand what each status code means in plain language
this.statusCode = code;
this.statusText = text || this.getDefaultStatusText(code);
}
getDefaultStatusText(code) {
const statusMessages = {
200: 'OK', // Like a waiter saying "Here's your order!"
201: 'Created', // "We've made that just for you"
301: 'Moved Permanently', // "We've moved to a new location"
400: 'Bad Request', // "Sorry, could you repeat that?"
401: 'Unauthorized', // "Could I see your ID please?"
403: 'Forbidden', // "Sorry, this area is staff only"
404: 'Not Found', // "I'm sorry, we don't have that item"
500: 'Internal Server Error', // "There's a problem in the kitchen"
503: 'Service Unavailable' // "We're temporarily closed"
};
return statusMessages[code] || 'Unknown Status';
}
getStatusLine() {
return `${this.version} ${this.statusCode} ${this.statusText}`;
}
}
// Let's see how we might use this in practice
function demonstrateStatusLines() {
const response = new HTTPResponse();
// Successfully found a resource
response.setStatus(200);
console.log('Success response:', response.getStatusLine());
// Resource not found
response.setStatus(404);
console.log('Not found response:', response.getStatusLine());
// Server maintenance
response.setStatus(503);
console.log('Maintenance response:', response.getStatusLine());
}