溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

Node.js如何實(shí)現(xiàn)hello world

發(fā)布時(shí)間:2021-11-22 09:22:48 來源:億速云 閱讀:131 作者:小新 欄目:web開發(fā)

這篇文章主要為大家展示了“Node.js如何實(shí)現(xiàn)hello world”,內(nèi)容簡(jiǎn)而易懂,條理清晰,希望能夠幫助大家解決疑惑,下面讓小編帶領(lǐng)大家一起研究并學(xué)習(xí)一下“Node.js如何實(shí)現(xiàn)hello world”這篇文章吧。

首先下載node.js,然后解壓到E盤,改名為node,然后開始菜單輸入cmd,用cd命令切換到nodejs的解壓目錄:

Node.js如何實(shí)現(xiàn)hello world

***個(gè)例子:hello world。

在node目錄下建立hello.js文件,然后在里面輸入:

var sys = require("sys");  sys.puts("Hello world");

然后我們?cè)诿_(tái)中輸入命令node hello.js,就能看到命名臺(tái)輸出結(jié)果Hello world。

第二個(gè)例子:hello world2。

好了,這次我們?cè)噺挠斡[器中輸出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/");

然后我們?cè)诿_(tái)中輸入命令node http.js,在瀏覽器輸入http://localhost:8080/

Node.js如何實(shí)現(xiàn)hello world
Node.js如何實(shí)現(xiàn)hello world

第三個(gè)例子:hello world2。

node.js提供一個(gè)Buffer類用于轉(zhuǎn)換不同編碼的字符串。目前支持三種類型:'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));

第四個(gè)例子: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如何實(shí)現(xiàn)hello world”這篇文章的所有內(nèi)容,感謝各位的閱讀!相信大家都有了一定的了解,希望分享的內(nèi)容對(duì)大家有所幫助,如果還想學(xué)習(xí)更多知識(shí),歡迎關(guān)注億速云行業(yè)資訊頻道!

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI