溫馨提示×

溫馨提示×

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

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

nodejs入門教程二:創(chuàng)建一個簡單應(yīng)用示例

發(fā)布時間:2020-09-11 06:23:39 來源:腳本之家 閱讀:119 作者:Dason_yu 欄目:web開發(fā)

本文實(shí)例講述了nodejs創(chuàng)建一個簡單應(yīng)用的方法。分享給大家供大家參考,具體如下:

1.創(chuàng)建 test.js

// require 來載入 http 模塊
var http = require('http');
/**
 * 使用 http.createServer() 方法創(chuàng)建服務(wù)器,返回 一個對象
 * 對象有一個叫做 listen 的方法,并使用 listen 方法綁定 8000 端口。
 * 函數(shù)通過 request, response 參數(shù)來接收和響應(yīng)數(shù)據(jù)。
 */
http.createServer(function (request, response) {
  response.writeHead(200, {'Content-Type': 'text/html; charset=utf-8'});
  if(request.url!=="/favicon.ico"){ //清除第2此訪問
    console.log('訪問');
    response.write('hello,world');
    response.end('hell,世界');//不寫 end() 則沒有http協(xié)議尾,但寫了會產(chǎn)生兩次訪問
  }
}).listen(8000);
console.log('Server running at http://127.0.0.1:8000/');

2. 執(zhí)行

node test.js

結(jié)果:Server  running  at  http://127.0.0.1:8000/

3. 在瀏覽器訪問 http://127.0.0.1:8000

希望本文所述對大家nodejs程序設(shè)計有所幫助。

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

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

AI