node web模塊 (服務器端和客戶端)

node web模塊html

web服務器

web服務器指網站服務器,指駐留在因特網上的某種程序,web瀏覽器的基本功能,提供信息瀏覽服務
web支持服務器端的腳本語言,經過腳本語言從數據庫獲取數據,將結果返回給客戶端瀏覽器node

web應用基本架構

Client => Server => Business => Data
Client 即客戶端,經過http協議向服務器發起請求
Server 服務器端,指web服務器,接收客戶端請求,並向客戶端發送響應的數據
Business 即業務層,經過Web服務器處理應用程序,數據庫的交互,邏輯運算,調用外部程序
Data 數據層,儲存數據

使用Node建立Web服務器

使用http模塊建立nginx

須要使用substr()方法,一個從指定位置返回指定結束的方法,繼承自String 最後一個參數可省 https://developer.mozilla.org...
// server.js
// 引入模塊
var http = require('http');    // http模塊
var fs = require('fs');        // fs 文件模塊
var url = require('url');    // url 統一資源定位符模塊

// 建立服務器
http.createServer((request, response) => {
    // 解析請求,保存進變量內
    var pathname = url.parse(request.url).pathname;

    // 輸出請求的文件名
    console.log(pathname);

    // 從文件系統中讀取文件,進行返回
    fs.readFile(pathname.substr(1), (err, data) => {    // 使用substr方法進行讀取文件的字節,將文件名返回給回調函數
        if (err){    // 對錯誤進行處理
            console.log(err);    // 打印出錯誤
            // 返回一個404
            response.writeHead(404, {'Content-Type': 'text/html; charset=utf-8'});
            response.write("呀,什麼都沒有╮(╯_╰)╭");
            response.write('我猜你要的是 ' + data + ' ?');
            response.end();
        } else {
            // 返回 200
            response.writeHead(200, {'Content-Type': 'text/html; charset=utf-8'});
            // 返回文件內容
            response.write(data.toString());    // 將讀取的文件內容進行字符串化,並進行輸出
            response.end();    // 關閉鏈接,發出數據
        };
    });
}).listen(1937);
// index.html
<!doctype html>
<html>
    <head>
        <meta charset="utf-8">
        <title>這是一個小Dome</title>
    </head>
    <body>
        <h1>hello word!</h1>
    </body>
</html>

訪問 http://127.0.0.1:1937/index.html
出現hello word完成!web

PS C:\Users\mingm\Desktop\test> node Server.js
/index.html
/
/input.html
/
{ [Error: ENOENT: no such file or directory, open 'C:\Users\mingm\Desktop\test\input.html']
  errno: -4058,
  code: 'ENOENT',
  syscall: 'open',
  path: 'C:\\Users\\mingm\\Desktop\\test\\input.html' }

改進一點

訪問/出現404,說明沒有設置首頁,設置首頁數據庫

// 增長對首頁的支持,設置默認首頁爲index.html
    if (pathname === '/') {
        pathname = pathname + 'index.html';
    }

完成後的文件以下瀏覽器

// 引入模塊
var http = require('http');    // http模塊
var fs = require('fs');        // fs 文件模塊
var url = require('url');    // url 統一資源定位符模塊

// 建立服務器
http.createServer((request, response) => {
    // 解析請求,保存進變量內
    var pathname = url.parse(request.url).pathname;

    // 輸出請求的文件名
    console.log(pathname);

    // 增長對首頁的支持,設置默認首頁爲index.html
    if (pathname === '/') {
        pathname = pathname + 'index.html';
    }

    // 從文件系統中讀取文件,進行返回
    fs.readFile(pathname.substr(1), (err, data) => {    // 使用substr方法進行讀取文件的字節,將文件名返回給回調函數
        if (err){    // 對錯誤進行處理
            console.log(err);    // 打印出錯誤
            // 返回一個404
            response.writeHead(404, {'Content-Type': 'text/html; charset=utf-8'});
            response.write("呀,什麼都沒有╮(╯_╰)╭");
            response.write('我猜你要的是&nbsp;' + data + '&nbsp;?');
            response.end();
        } else {
            // 返回 200
            response.writeHead(200, {'Content-Type': 'text/html; charset=utf-8'});
            // 返回文件內容
            response.write(data.toString());    // 將讀取的文件內容進行字符串化,並進行輸出
            response.end();    // 關閉鏈接,發出數據
        };
    });
}).listen(1937);

使用node建立客戶端

PS C:\Users\mingm\Desktop\test> node get.js
<html>
<head><title>302 Found</title></head>
<body bgcolor="white">
<center><h1>302 Found</h1></center>
<hr><center>nginx</center>
</body>
</html>

PS C:\Users\mingm\Desktop\test>
var http = require('http');

// 請求的選項
var options = {    // 建立一個對象保存相關數據
    host:'www.iming.info',    // 主機地址
    port:'443',    // 訪問端口
    method:'GET',
    path:'/',    // 訪問的文件
};

// 處理響應的回調函數
var callback = (response) => {
    // 更新數據
    var body = '';
    response.on('data', (data) => {        // 綁定事件,data
        body += data;    
    });

    response.on('end', () => {    // 綁定end事件
        console.log(body);    
    });
};

// 開始發送請求
var req = http.request(options, callback);    // 發送請求,options爲發送請求的選項,callback爲處理請求的回調函數,將會有拋出三個事件一個data一個end,一個error,必須有end表示請求完畢,關閉鏈接
req.end();    // 關閉鏈接

因爲小站使用的是nginx的https,使用了證書,須要使用驗證祕鑰的過程,因此拒絕訪問,302沒有權限服務器

相關文章
相關標籤/搜索