// server.js var http = require("http"); http.createServer(function(request, response) { response.writeHead(200, {"Content-Type": "text/plain"}); response.write("Hello World"); response.end(); }).listen(8888); // node server.js // 打開http://localhost:8888/,你會看到一個寫着「Hello World」的網頁~
首先咱們來看看服務端模式下如何工做:html
HTTP請求本質上是一個數據流,由請求頭(headers)和請求體(body)組成node
// 請求頭 POST / HTTP/1.1 User-Agent: curl/7.26.0 Host: localhost Accept: */* Content-Length: 11 Content-Type: application/x-www-form-urlencoded // 請求體 Hello World
HTTP請求在發送給服務器時,能夠認爲是按照從頭至尾的順序一個字節一個字節地以數據流方式發送的。而http模塊建立的HTTP服務器在接收到完整的請求頭後,就會調用回調函數。在回調函數中,可使用request對象訪問請求頭數據並把request對象看成一個只讀數據流來訪問請求體數據git
可修改上述代碼github
// server.js var http = require("http"); http.createServer(function(request, response) { // 讀取 requst 數據流 console.log('我是請求方法:', request.method) console.log('我是請求頭:', request.headers) request.on('data', function (chunk) { body.push(chunk); }); request.on('end', function () { body = Buffer.concat(body); console.log('我是請求體呀', body.toString()); }); // 處理response response.writeHead(200, {"Content-Type": "text/plain"}); response.write("Hello World"); response.end(); }).listen(8888); // node server.js // 打開http://localhost:8888/,你會看到一個寫着「Hello World」的網頁~
就這麼簡單,本文 完~web
oh,no
you too young, too simple瀏覽器
var http = require("http")
createServer
createServer
的參數
請求處理服務器
onRequest() 函數被觸發的時候,有兩個參數對象responseapp
// 發送一個HTTP狀態200和HTTP頭的內容類型 response.writeHead(200, {"Content-Type": "text/plain"}); // 添加HTTP主體內容 response.write("Hello World"); // 完成響應 response.end();
這一步咱們把server.js變成一個真正的Node.js模塊curl
函數封裝
將咱們的腳本封裝到一個函數裏面,而後導出該封裝函數函數
var http = require("http"); function start() { function onRequest(request, response) { console.log("Request received."); response.writeHead(200, {"Content-Type": "text/plain"}); response.write("Hello World"); response.end(); } http.createServer(onRequest).listen(8888); console.log("Server has started."); } exports.start = start;
模塊引用
// 如主文件名爲index.js,寫入 var server = require("./server"); server.start();
執行 node index.js
全部請求數據都在 request對象中,數據解析,還須要 url, querystring模塊
來,咱們試一試找出瀏覽器的請求路徑~
var http = require("http"); var url = require('url') function start(){ function onRequest(req, res){ var url = url.parse(req.url) // 打印 url 信息 console.log('server start url', url) res.writeHead(200, {"content-type": "text/plain"}) res.end() } http.createServer(onRequest).listen(8888) } exports.start = start
request.url參數打印:
引入路由處理
// server.js var http = require("http"); var url = require('url') function start(route){ function onRequest(req, res){ var pathname = url.parse(req.url).pathname route(pathname) res.writeHead(200, {"content-type": "text/plain"}) res.end() } http.createServer(onRequest).listen(8888) } exports.start = start
// route.js function route(pathname){ console.log('route', pathname) } exports.route = route // index.js 引入route var server = require('./server') var router = require('./route') server.start(router.route)
以上代碼咱們實現了有路可尋
爲了不多重的 if..else..,咱們經過對象傳遞一系列請求
server 中調用 route 的處理結果
// requestManager.js function start(){ console.log('route-----start') return 'hello start' } function next(){ console.log('route-----next') return 'hello next' } exports.start = start exports.next = next
// index.js var server = require('./readfile') var router = require('./route') var requestManager = require('./requestManager') var managers = [] managers['/'] = requestManager.start managers['/start'] = requestManager.start managers['/next'] = requestManager.next server.start(router.route, managers) // http://localhost:8888/start, 瀏覽器會輸出「hello start」 // http://localhost:8888/next 會輸出「hello next」 // http://localhost:8888/chaoran 會輸出「404」。
manager :每個路由提供對應的處理函數
// server.js var http = require("http"); var url = require('url') function start(route, manager){ function onRequest(req, res){ var pathname = url.parse(req.url).pathname console.log('server request for', pathname) var content = route(pathname, manager) res.writeHead(200, {"content-type": "text/plain"}) res.write(content) res.end() } http.createServer(onRequest).listen(8888) } exports.start = start
取出managers 中的路由事件進行處理
// route.js function route(pathname, managers){ console.log('rrr', pathname) if(typeof managers[pathname] == 'function'){ return managers[pathname]() }else { console.log('managers no found') return '' } } exports.route = route
好啦,用是能用的,就是偶爾會掛 ( ﹁ ﹁ ) ~→
至於node中的並行及如何實現非阻塞,下次咱們結合實例來學習~
參考: