const http = require('http');
const url = require('url');
// Create an HTTP server that demonstrates different status codes
const server = http.createServer((req, res) => {
const parsedUrl = url.parse(req.url, true);
const path = parsedUrl.pathname;
// Remove any trailing slashes and normalize the path
const normalizedPath = path.replace(/\/+$/, '');
// Route based on the path
if (normalizedPath === '') {
// 200 OK - Success response
res.writeHead(200, { 'Content-Type': 'text/html' });
res.end(`
<!DOCTYPE html>
<html>
<head><title>Status Code Demo</title></head>
<body>
<h1>HTTP Status Code Demo</h1>
<style>
body { font-family: Arial, sans-serif; max-width: 800px; margin: 0 auto; padding: 20px; }
.success { color: #2ecc71; }
.redirect { color: #f39c12; }
.client-error { color: #e74c3c; }
.server-error { color: #9b59b6; }
.info { color: #3498db; }
.status-box {
padding: 15px;
margin: 10px 0;
border-left: 5px solid;
border-radius: 4px;
background-color: #f9f9f9;
}
a { color: #3498db; text-decoration: none; }
a:hover { text-decoration: underline; }
</style>
</head>
<body>
<h1>HTTP Status Code Demo</h1>
<p>Click on the links below to see different HTTP status codes in action:</p>
<h2>Success (2xx)</h2>
<div class="status-box success">
<a href="/success">200 OK</a> - Standard response for successful HTTP requests.
</div>
<div class="status-box success">
<a href="/created">201 Created</a> - Request has been fulfilled, resulting in the creation of a new resource.
</div>
<h2>Redirection (3xx)</h2>
<div class="status-box redirect">
<a href="/moved">301 Moved Permanently</a> - This and all future requests should be directed to the given URI.
</div>
<div class="status-box redirect">
<a href="/temporary">307 Temporary Redirect</a> - The request should be repeated with another URI, but future requests should still use the original URI.
</div>
<h2>Client Errors (4xx)</h2>
<div class="status-box client-error">
<a href="/bad-request">400 Bad Request</a> - The server cannot or will not process the request due to an apparent client error.
</div>
<div class="status-box client-error">
<a href="/forbidden">403 Forbidden</a> - The request was valid, but the server is refusing action.
</div>
<div class="status-box client-error">
<a href="/not-found">404 Not Found</a> - The requested resource could not be found.
</div>
<h2>Server Errors (5xx)</h2>
<div class="status-box server-error">
<a href="/error">500 Internal Server Error</a> - An error occurred on the server.
</div>
<div class="status-box server-error">
<a href="/unavailable">503 Service Unavailable</a> - The server is currently unavailable.
</div>
<h2>Information (1xx)</h2>
<div class="status-box info">
<a href="/continue">100 Continue</a> - The server has received the request headers and the client should proceed to send the request body.
</div>
</body>
</html>
`);
}
else if (normalizedPath === '/success') {
// 200 OK
res.writeHead(200, 'OK', { 'Content-Type': 'text/plain' });
res.end('200 OK - The request has succeeded.');
}
else if (normalizedPath === '/created') {
// 201 Created
res.writeHead(201, 'Created', { 'Content-Type': 'text/plain' });
res.end('201 Created - A new resource has been created.');
}
else if (normalizedPath === '/moved') {
// 301 Moved Permanently
res.writeHead(301, { 'Location': '/success' });
res.end();
}
else if (normalizedPath === '/temporary') {
// 307 Temporary Redirect
res.writeHead(307, { 'Location': '/success' });
res.end();
}
else if (normalizedPath === '/bad-request') {
// 400 Bad Request
res.writeHead(400, 'Bad Request', { 'Content-Type': 'text/plain' });
res.end('400 Bad Request - The server cannot process the request due to a client error.');
}
else if (normalizedPath === '/forbidden') {
// 403 Forbidden
res.writeHead(403, 'Forbidden', { 'Content-Type': 'text/plain' });
res.end('403 Forbidden - You do not have permission to access this resource.');
}
else if (normalizedPath === '/not-found') {
// 404 Not Found
res.writeHead(404, 'Not Found', { 'Content-Type': 'text/plain' });
res.end('404 Not Found - The requested resource could not be found.');
}
else if (normalizedPath === '/error') {
// 500 Internal Server Error
res.writeHead(500, 'Internal Server Error', { 'Content-Type': 'text/plain' });
res.end('500 Internal Server Error - An error occurred on the server.');
}
else if (normalizedPath === '/unavailable') {
// 503 Service Unavailable
res.writeHead(503, 'Service Unavailable', { 'Content-Type': 'text/plain' });
res.end('503 Service Unavailable - The server is currently unavailable.');
}
else if (normalizedPath === '/continue') {
// 100 Continue
res.writeContinue();
res.writeHead(200, { 'Content-Type': 'text/plain' });
res.end('100 Continue - The server has received the request headers.');
}
else {
// 404 Not Found for any other path
res.writeHead(404, 'Not Found', { 'Content-Type': 'text/plain' });
res.end('404 Not Found - The requested resource could not be found.');
}
});
// Start the server
const PORT = 3002;
server.listen(PORT, () => {
console.log(`Server running at http://localhost:${PORT}/`);
console.log(`Visit http://localhost:${PORT} in your browser to see the demo.`);
});
// Handle server errors
server.on('error', (err) => {
console.error('Server error:', err);
});