NodeJS系列~目(mu)錄
Node.js官網對它的闡述
Node.js is a platform built on for easily building fast, scalable network applications. Node.js uses an event-driven, non-blocking I/O model that makes it lightweight and efficient, perfect for data-intensive real-time applications that run across distributed devices.
Node.js是一個運行在chrome腳本引擎上的(de)應用程序,它是基于事件驅動,單純(chun)種,非阻塞(sai)的(de)輕量(liang)級的(de),高效的(de)應用程序,它可以(yi)用做搭建WEB服(fu)務器上.
Node.js官網的一個(ge)類似hello world的實(shi)例(li)
var http = require('http'); http.createServer(function (req, res) { res.writeHead(200, {'Content-Type': 'text/plain'}); res.end('Hello World\n'); }).listen(1337, '127.0.0.1'); console.log('Server running at //127.0.0.1:1337/');
上面代(dai)碼的(de)功能是開啟一個(ge)端(duan)口(kou)1337來進行http協(xie)議的(de)監聽,當有(you)客戶端(duan)發出請求(request)時(shi),node.js會做出一個(ge)響應(ying)(resonse),結果是在客戶端(duan)的(de)瀏(liu)覽器(qi)上輸出Hello World字符!
Node.js不僅(jin)可以創建http服(fu)務,而且還可以創建基于tcp的服(fu)務,下面(mian)是一(yi)個socket通訊的例子,向客戶端開啟1337端口,進行(xing)對tcp協(xie)議的監聽(ting)
var net = require('net'); var server = net.createServer(function (socket) { socket.write('Echo server\r\n'); socket.pipe(socket); }); server.listen(1337, '127.0.0.1');
我(wo)們在書(shu)寫代(dai)碼時,可以看到(dao),node.js像(xiang)其它(ta)語言一樣,都有自(zi)己的引用(yong)關鍵字,即將(jiang)一些類庫(ku)引入到(dao)當(dang)前服務中(zhong)來,在node.js里require就是這個(ge)關鍵字,它(ta)實現(xian)了對類庫(ku)的引用(yong).
好了,如果大家希望學習(xi)更(geng)多的(de)node.js知識(shi),可以閱(yue)讀我這個系列的(de)文章!
NodeJS系列~目錄
第一個小例子,實現了request.querystring功能
永久更新中...