系統(tǒng)之家 - 系統(tǒng)光盤下載網(wǎng)站!

當(dāng)前位置:系統(tǒng)之家 > 系統(tǒng)教程 > Ubuntu系統(tǒng)使用Node.js運(yùn)行Hello world

Ubuntu系統(tǒng)使用Node.js運(yùn)行Hello world的方法

時(shí)間:2015-07-14 15:08:24 作者:zhijie 來源:系統(tǒng)之家 1. 掃描二維碼隨時(shí)看資訊 2. 請(qǐng)使用手機(jī)瀏覽器訪問: https://m.xitongzhijia.net/xtjc/20150714/52866.html 手機(jī)查看 評(píng)論

  Node.js為Linux系統(tǒng)提供了豐富的各種模塊的JavaScript庫,從而簡(jiǎn)化了使用Node.js,一個(gè)很大的程度上方便web應(yīng)用程序的研究與開發(fā)。本文就來介紹一下Node.js一個(gè)最基礎(chǔ)的知識(shí),那就是在Ubuntu中運(yùn)行Hello world。

Ubuntu系統(tǒng)使用Node.js運(yùn)行Hello world的方法

  安裝好node.js后,在任意路徑新建文件helloworld.js,輸入如下內(nèi)容:

  console.log(“Hello world!”)

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

  node helloworld.js

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

  以上就是node的最簡(jiǎn)單的一個(gè)hello world程序。node.js作為服務(wù)器編程技術(shù),另一個(gè)基本的hello world程序就是在網(wǎng)頁上輸出Hello world.

  將helloworld.js的內(nèi)容改成如下內(nèi)容:

  var http =require(“http”);//獲取http對(duì)象

  //利用http對(duì)象的createServer函數(shù)創(chuàng)建一個(gè)server對(duì)象,參數(shù)是一個(gè)無參函數(shù),函數(shù)利用response對(duì)象向瀏覽器寫入頭信息和文本

  var server = http.createServer(function(request, response) {

  response.writeHead(200, {“Content-Type”:“text/plain”});

  response.write(“Hello World”);

  response.end();

  });

  //server對(duì)象監(jiān)聽8888端口

  server.listen(8888);

  然后再執(zhí)行node helloworld.js,然后在瀏覽器中訪問https://localhost:8888/

  瀏覽器中就會(huì)顯示Hello world!

  Ubuntu系統(tǒng)使用Node.js運(yùn)行Hello world的介紹了,只要學(xué)會(huì)這個(gè)技巧,就算是對(duì)Node.js開始入門了。

標(biāo)簽 Node.js

發(fā)表評(píng)論

0

沒有更多評(píng)論了

評(píng)論就這些咯,讓大家也知道你的獨(dú)特見解

立即評(píng)論

以上留言僅代表用戶個(gè)人觀點(diǎn),不代表系統(tǒng)之家立場(chǎng)

其他版本軟件

熱門教程

人氣教程排行

Linux系統(tǒng)推薦

掃碼關(guān)注
掃碼關(guān)注

掃碼關(guān)注 官方交流群 軟件收錄