歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux基礎 >> Linux教程 >> Node.js入門基礎知識:Hello world!

Node.js入門基礎知識:Hello world!

日期:2017/2/28 14:00:17   编辑:Linux教程

Node.js在Ubuntu中的安裝過程在本站有許多介紹了。比如下面這篇

Ubuntu 14.04下搭建Node.js開發環境 http://www.linuxidc.com/Linux/2014-12/110983.htm

安裝好node.js後,在任意路徑新建文件helloworld.js,輸入如下內容:

console.log("Hello world!")

然後在該路徑下啟動Terminal(Windows中就是命令行Command Line),或者先啟動Terminal再使用cd命令切換到該路徑也行(Windows同理),然後輸入命令

node helloworld.js

回車,窗口中會輸出“Hello world!”字樣。

以上就是node的最簡單的一個hello world程序。node.js作為服務器編程技術,另一個基本的hello world程序就是在網頁上輸出Hello world.

將helloworld.js的內容改成如下內容:

var http =require("http");//獲取http對象

//利用http對象的createServer函數創建一個server對象,參數是一個無參函數,函數利用response對象向浏覽器寫入頭信息和文本
var server = http.createServer(function(request, response) {
response.writeHead(200, {"Content-Type":"text/plain"});
response.write("Hello World");
response.end();
});

//server對象監聽8888端口
server.listen(8888);

然後再執行node helloworld.js,然後在浏覽器中訪問http://localhost:8888/

浏覽器中就會顯示Hello world!

下面的內容你可能也喜歡

如何在CentOS 7安裝Node.js http://www.linuxidc.com/Linux/2015-02/113554.htm

Ubunru 12.04 下Node.js開發環境的安裝配置 http://www.linuxidc.com/Linux/2014-05/101418.htm

Node.Js入門[PDF+相關代碼] http://www.linuxidc.com/Linux/2013-06/85462.htm

Node.js開發指南 高清PDF中文版 +源碼 http://www.linuxidc.com/Linux/2014-09/106494.htm

Node.js入門開發指南中文版 http://www.linuxidc.com/Linux/2012-11/73363.htm

Ubuntu 編譯安裝Node.js http://www.linuxidc.com/Linux/2013-10/91321.htm

Node.js 的詳細介紹:請點這裡
Node.js 的下載地址:請點這裡

Copyright © Linux教程網 All Rights Reserved