安裝
1.更新庫: sudo apt-get update
2.安裝nodejs: sudo apt-get install nodejs
3.安裝npm: sudo apt-get install npm
如果裝好了,輸入node -v可以查看node版本:

image.png
輸入npm -v可查看npm版本:

image.png
Hello World
1.創(chuàng)建一個(gè)文件夾用來放這些學(xué)習(xí)用的文件: mkdir nodejs-learning
2.創(chuàng)建一個(gè)node文件: touch helloWorld.js
3.在helloWorld.js中寫入以下代碼:
var http = require('http');
http.createServer(function(req, res) {
res.writeHead(200, {'Content-Type': 'text/html'});
res.end('Hello world');
}).listen(8080);
4.讓這個(gè)文件跑起來: 在命令行里輸入node helloWorld.js,發(fā)現(xiàn)沒啥提示(正常)。
5.打開http://localhost:8080/:成功看到Hello world。

image.png
分析
1.var http = require('http');:NodeJs有很多內(nèi)建模塊,http就是其中的一個(gè),通過require來將其引用進(jìn)來。
2.createServer是http模塊里的方法,創(chuàng)建一個(gè)服務(wù)器,參數(shù)為一個(gè)回調(diào)函數(shù),當(dāng)客戶端訪問該服務(wù)器時(shí),執(zhí)行回調(diào)函數(shù)。req是request的縮寫,res是response的縮寫。
3.writeHead:分別寫入狀態(tài)碼和頭文件中的字段。