Node.js 組成:javascript
一、required模塊:使用require指令來載入Node.js模塊;java
二、建立服務器:服務器用於監聽客服端的請求,相似於apache等http服務器。node
三、接收請求與響應請求:客戶端使用瀏覽器或終端發送http請求,服務器接收請求後返回響應數據。apache
建立 Node.js 第一個 "Hello, World!" 應用。瀏覽器
var http = require('http');
var http = require('http'); http.createServer(function(request,response){ //發送http頭部 //http狀態值:200:ok //內容類型:text/plain (plain顯示代碼段) response.writeHead(200,{'Content-Type':'text/plain'}); //發送響應數據「hello world」 response.end('hello world'); }).listen(8888); //終端打印信息 console.log('Server running at http://127.0.0.1:8888/');
node命令執行上面的代碼:服務器
node server.js Server running at http://127.0.0.1:8888/
使用瀏覽器訪問http://127.0.0.1:8888/ 瀏覽器將輸入‘hello world’;函數