Search

Airport Airline Lounges

2 min read 0 views
Airport Airline Lounges

...

Introduction

...

© 2024 Airline Lounges Guide. All rights reserved.

`; // Create an HTTP server const server = http.createServer((req, res) => { if (req.url === '/' || req.url === '/airline-lounges-guide') {
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 http module: 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.

Alternative: HTTPS Server

If you need to serve the page over HTTPS, use the `https` module and provide TLS certificates: js const https = require('https'); const fs = require('fs'); const options = { key: fs.readFileSync('path/to/key.pem'), cert: fs.readFileSync('path/to/cert.pem') }; https.createServer(options, (req, res) => { // Similar handling as above });

Summary

The original code attempted to misuse the `https` module for sending HTML content, resulting in a misunderstanding of how Node.js handles client requests versus server responses. The corrected implementation demonstrates a standard approach for serving an HTML page in Node.js.
Was this helpful?

Share this article

Suggest a Correction

Found an error or have a suggestion? Let us know and we'll review it.

Comments (0)

Please sign in to leave a comment.

No comments yet. Be the first to comment!