歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux編程 >> Linux編程 >> Node.js零起點開發教程

Node.js零起點開發教程

日期:2017/3/1 10:08:26   编辑:Linux編程

安裝node.js

參考鏈接:https://github.com/joyent/node/wiki/Installation

安裝非編譯版本

這裡面提供的是在不同操作系統上編譯node.js的方法,參照鏈接https://github.com/joyent/node/wiki/Installation

安裝編譯後的版本

現在地址:http://nodejs.org/download/

我的機器是windows的,選擇的文件是,是編譯後的版本:Windows Installer (.msi) 32-bit

我本人用的是編譯好後的版本,安裝好後的效果

Hello World

打開你最喜歡的編輯器,創建一個helloworld.js文件,輸入console.log("Hello World");後保存

打開Node.js的CMD窗口,將文件拖拽到窗口中,輸入node js文件路徑

Http版Hello World

編寫 helloworld.js 代碼如下:

  1. var http = require('http');
  2. server = http.createServer(function (req, res) {
  3. res.writeHeader(200, {"Content-Type": "text/plain"});
  4. res.end("Hello World\n");
  5. })
  6. server.listen(8000);
  7. console.log("httpd start @8000");

運行 node helloworld.js按照上面node.js cmd的方法,控制台顯示 httpd start @8000
打開浏覽器訪問 http://localhost:8000/ 浏覽器顯示 Hello World

Copyright © Linux教程網 All Rights Reserved