Web Sunucusu Başlatma

Web Sunucusu Başlatma

NodeJS uygulaması temel olarak üç önemli bileşeni içermelidir.

  1. Önemli/Gerekli modülleri (require yönergesi ile yapılır.)
  2. Sunucu oluşturma
  3. Talepleri ve yanıtları dinleme, değerlendirme

Sunucu oluşturma yapısı temel olarak aşağıdaki gibidir.

var http = require("http");

http.createServer(function (request, response) {
   // Send the HTTP header 
   // HTTP Status: 200 : OK
   // Content Type: text/plain
   response.writeHead(200, {'Content-Type': 'text/plain'});
   
   // Send the response body as "Hello World"
   response.end('Hello World\n');
}).listen(8081);

// Console will print the message
console.log('Server running at http://127.0.0.1:8081/');

Eğer yukarıdaki kodu “test.js” dosya adıyla kaydedilirse aşağıdaki ifade ile çalıştırılabilir.

node main.js