您好,登錄后才能下訂單哦!
本文實(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è)計有所幫助。
免責(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)容。