01.Nodejs入門之Helloworld

說明:本文章可供有必定js基礎的朋友參考nodejs入門,本文未講解nodejs的安裝,若有須要的同窗能夠加QQ3382260752找我,進行交流學習。node

 

1.新建文件夾helloworld demo,而後新建文件server.js,將如下代碼拷貝至server.js瀏覽器

var http = require('http'); 
http.createServer(function (request, response) {服務器


//request 能夠簡寫req,  response能夠簡寫爲res學習

// 發送 HTTP 頭部 
// HTTP 狀態值: 200 : OK
// 內容類型: text/plainui


response.writeHead(200, {'Content-Type': 'text/plain'});命令行

// 發送響應數據 "Hello World"server


response.end('Hello NodeJS\n');
}).listen(1338);cmd


// 控制檯打印以下信息
console.log('Server running at http://127.0.0.1:1338/');it


2.打開命令行 (快速打開能夠在helloworld demo 地址欄 直接輸入cmd快速進入命令窗口)io

3.在命令行輸入:node server.js 即會出現 「Server running at http://127.0.0.1:1338/」

4.打開瀏覽器,在地址欄輸入:http:127.0.0.1:1338運行 便可出現 Hello NodeJs

  若是修改了server.js裏面 響應的內容,需重啓服務器,而後從新刷新瀏覽器便可

相關文章
相關標籤/搜索