這篇文章主要為大家展示了“Node.js如何實現hello world”,內容簡而易懂,條理清晰,希望能夠幫助大家解決疑惑,下面讓小編帶領大家一起研究并學習一下“Node.js如何實現hello world”這篇文章吧。
首先下載node.js,然后解壓到E盤,改名為node,然后開始菜單輸入cmd,用cd命令切換到nodejs的解壓目錄:
***個例子:hello world。
在node目錄下建立hello.js文件,然后在里面輸入:
var sys = require("sys"); sys.puts("Hello world");
然后我們在命名臺中輸入命令node hello.js,就能看到命名臺輸出結果Hello world。
第二個例子:hello world2。
好了,這次我們試從游覽器中輸出hello world。在node目錄下建立http.js,然后輸入:
var sys = require("sys"), http = require("http"); http.createServer(function(request, response) { response.sendHeader(200, {"Content-Type": "text/html"}); response.write("Hello World!"); response.close(); }).listen(8080); sys.puts("Server running at http://localhost:8080/");
然后我們在命名臺中輸入命令node http.js,在瀏覽器輸入http://localhost:8080/
第三個例子:hello world2。
node.js提供一個Buffer類用于轉換不同編碼的字符串。目前支持三種類型:'ascii','utf8'與'binary'。詳見這里
var Buffer = require('buffer').Buffer, buf = new Buffer(256), len = buf.write('\u00bd + \u00bc = \u00be', 0); console.log(len + " bytes: " + buf.toString('utf8', 0, len));
第四個例子:hello world3。
//synopsis.js //synopsis 摘要, 梗概,大綱 var http = require('http'); http.createServer(function (request, response) { response.writeHead(200, {'Content-Type': 'text/plain'}); response.end('Hello World\n'); }).listen(8124); console.log('Server running at http://127.0.0.1:8124/');
以上是“Node.js如何實現hello world”這篇文章的所有內容,感謝各位的閱讀!相信大家都有了一定的了解,希望分享的內容對大家有所幫助,如果還想學習更多知識,歡迎關注億速云行業資訊頻道!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。