天天看點

實作本地簡單web伺服器

伺服器端:

var http = require(\'http\');

http.createServer(function (request, response) {

	// 發送 HTTP 頭部 
	// HTTP 狀态值: 200 : OK
	// 内容類型: text/plain
	response.writeHead(200, {\'Content-Type\': \'text/plain\'});

	// 發送響應資料 "Hello World"
	response.end(\'Hello World\n\');
}).listen(8888);

// 終端列印如下資訊
console.log(\'Server running at http://127.0.0.1:8888/\');
      

  

本地端:浏覽器url輸入:127.0.0.1:8888, xiao\'guo效果如下圖:

實作本地簡單web伺服器
實作本地簡單web伺服器