Node.js官網對它的闡述
Node.js is a platform built on Chrome's JavaScript runtime 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腳本引擎上的應用程序,它是基于事件驅動,單純種,非阻塞的輕量級的,高效的應用程序,它可以用做搭建WEB服務器上.
Node.js官網的一個類似hello world的實例
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 http://127.0.0.1:1337/');
上面代碼的功能是開啟一個端口1337來進行http協議的監聽,當有客戶端發出請求(request)時,node.js會做出一個響應(resonse),結果是在客戶端的瀏覽器上輸出Hello World字符!
Node.js不僅可以創建http服務,而且還可以創建基于tcp的服務,下面是一個socket通訊的例子,向客戶端開啟1337端口,進行對tcp協議的監聽
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');
我們在書寫代碼時,可以看到,node.js像其它語言一樣,都有自己的引用關鍵字,即將一些類庫引入到當前服務中來,在node.js里require就是這個關鍵字,它實現了對類庫的引用.
好了,如果大家希望學習更多的node.js知識,可以閱讀我這個系列的文章!
NodeJS系列~目錄
第一個小例子,實現了request.querystring功能
永久更新中...
文章列表