...
Introduction
...res.statusCode = 200;
res.setHeader('Content-Type', 'text/html');
res.setHeader('Content-Length', Buffer.byteLength(airlineLoungeHTML, 'utf8'));
res.end(airlineLoungeHTML);
} else {
// Handle 404 Not Found
res.statusCode = 404;
res.setHeader('Content-Type', 'text/plain');
res.end('404 Not Found');
}
});
server.listen(port, hostname, () => {
console.log(`Server running at http://${hostname}:${port}/`);
});
Key Changes
- Using
httpmodule: The server listens on a port and responds to incoming requests. - Content-Type and Content-Length are set correctly on the server response.
- Response handling: The server writes the HTML string to the response and ends it.
- Simplified request logic: The server only serves the page at
/or a specific path, handling other routes with a 404.
No comments yet. Be the first to comment!