Node構(gòu)建Web服務(wù)器

Web模塊

  • web服務(wù)器,網(wǎng)站服務(wù)器
  • 提供Web信息瀏覽服務(wù);只支持HTTP協(xié)議、Html文檔格式及URL,與客戶端的網(wǎng)絡(luò)瀏覽器配合
  • web服務(wù)器 = 服務(wù)端的腳本語言(php/ruby/python) + 從數(shù)據(jù)庫獲取數(shù)據(jù) + 返回結(jié)果給客戶瀏覽器
  • 最主流Web服務(wù)器 Apache/ Nginx/ IIS
  • Web應(yīng)用架構(gòu)
    • Client 客戶端:瀏覽器
    • Server 服務(wù)端: Web服務(wù)器
    • Business 業(yè)務(wù)層: 與數(shù)據(jù)交互、邏輯運(yùn)算、調(diào)用外部程序 (位于web服務(wù)器上的處理應(yīng)用程序
    • Data 數(shù)據(jù)層:數(shù)據(jù)庫

使用node構(gòu)建一個(gè)服務(wù)端

  1. server.js //http服務(wù)器搭建
  2. router.js //路由模塊
  3. index.js //松散添加路由
  1. server.js此處構(gòu)建一個(gè)服務(wù)器,可在此扔入靜態(tài)頁面,就可以進(jìn)行訪問
var http = require('http');
var url = require('url');
var fs = require("fs");

function start(route){  
    function onRequest(request,response){
           // 解析請求,包括文件名
           var pathname = url.parse(request.url).pathname;
           
           // 輸出請求的文件名
           console.log("Request for " + pathname + " received.");
           
           //路由調(diào)用
           route(pathname);
           
            // 從文件系統(tǒng)中讀取請求的文件內(nèi)容
           fs.readFile(pathname.substr(1), function (err, data) {
              if (err) {
                 console.log(err);
                 // HTTP 狀態(tài)碼: 404 : NOT FOUND
                 // Content Type: text/plain
                 response.writeHead(404, {'Content-Type': 'text/html'});
              }else{             
                 // HTTP 狀態(tài)碼: 200 : OK
                 // Content Type: text/plain
                 response.writeHead(200, {'Content-Type': 'text/html'});    
                 
                 // 響應(yīng)文件內(nèi)容
                 response.write(data.toString());        
              }
              //  發(fā)送響應(yīng)數(shù)據(jù)
              response.end();
           });              
    }
    // 創(chuàng)建服務(wù)器
    http.createServer(onRequest).listen(8080);
     
    // 控制臺(tái)會(huì)輸出以下信息
    console.log('Server running at http://127.0.0.1:8080/');
}
exports.start = start;
  1. router.js :路由導(dǎo)航
function route(pathname){
  console.log("About to route a request for " + pathname);
}   
exports.route = route;
  1. index.js 路由服務(wù)注入服務(wù)器的控制文件
var server = require("./server");
var router = require("./router");
server.start(router.route);

此時(shí)server.js文件所在文件地址就是http://127.0.0.1:8080/
http://127.0.0.1:8080/index.html就是指與server.js文件同地址下的index.html文件

使用node構(gòu)建一個(gè)客戶端

  1. client.js 相當(dāng)于創(chuàng)建一個(gè)瀏覽器
var http = require('http');

//用于請求的選項(xiàng)
var options={
    host:'localhost',
    port:'8080',
    path:'/index.html'
}

//處理響應(yīng)的回調(diào)函數(shù)
var callback = function(response){
    //不斷更新數(shù)據(jù)
    var body = '';
    response.on('data',function(data){
        body += data;
    })
    response.on('end',function(){
        //數(shù)據(jù)接收完成
    })
}

//向服務(wù)端發(fā)送請求
var req = http.request(options,callback);
req.end();
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容