User Tools

Site Tools


njsetn:building_a_web_server

→ nodejs.org/api/http.html

→ nodejs.org/api/https.html

var require("http");

var server = http.createServer(function(req, res) {
  res.writeHead(200, {"Content-Type": "text/plain"});
  // 200 : successful // text/html for html
  res.end("Hello World");
});

server.listen(3000); // port 3000

console.log("Server listening on port 3000");
(...)
var server = http.createServer(function(req, res) {
  res.writeHead(200, {"Content-Type": "text/html"});
  res.end(`
    <!DOCTYPE html>
    <html>
      <head>
        <title>HTML Response</title>
        <p>${req.url}</p>
        <p>${req.method}</p>
      </head>
      <body>
        <h1>Serving HTML text</h1>
      </body>
    </html>
  `);
});
(...)
njsetn/building_a_web_server.txt · Last modified: 2016/03/25 20:26 by leo