国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > JavaScript > 正文

從零開始學習Node.js系列教程之設置HTTP頭的方法示例

2019-11-19 16:49:55
字體:
來源:轉載
供稿:網友

本文實例講述了Node.js設置HTTP頭的方法。分享給大家供大家參考,具體如下:

server.js

//basic server的配置文件var port = 3000;var server = require('./basicserver').createServer();server.useFavIcon("localhost", "./docroot/favicon.png");server.addContainer(".*", "/l/(.*)$", require('./redirector'), {})server.docroot("localhost", "/", "./docroot");//server.useFavIcon("127.0.0.1", "./docroot/favicon.png");//server.docroot("127.0.0.1", "/", "./docroot");server.listen(port);

basicserver.js

Response Header 服務器發送到客戶端

文件擴展名不足以完全恰當的標識文件類型,而且文件擴展名沒有標準,于是,人們設計了Content-Type頭和整個MIME類型標準來作為數據類型的表示系統。

對于某些應用,特別是一些處理固定數據的小型應用,我們可以精準的知道該使用哪一種Content-Type頭,因為應用發送的數據是特定已知的。然而staticHandler能發送任何文件,通常不知道該使用哪種Content-Type。通過匹配文件擴展名列表和Content-Type可以解決這個問題,但是這個方案不完美。最好的實踐方案是使用一個外部的配置文件,它通常由操作系統提供。

MIME npm包使用了Apache項目的mime.types文件,該文件包含超過600個Content-Type的有關數據,如果有需要,mime模塊也支持添加自定義的MIME類型。

npm install mime

var mime = require('mime');var mimeType = mime.lookup('image.gif'); //==> image/gifres.setHeader('Content-Type', mimeType);

一些相關的HTTP頭:

Content-Encoding 數據被編碼時使用,例如gzip
Content-Language 內容中使用的語言
Content-Length 字節數
Content-Location 能取到數據的一個候補位置
Content-MD5 內容主題的MD5校驗和

HTTP協議是無狀態的,意味著web服務器不能辨認不同的請求發送端。現在普遍的做法是,服務器發送cookie到客戶端瀏覽器,cookie中定義了登陸用戶的身份,對于每一次請求,web瀏覽器都會發送對應所訪問網站的cookie。

發送cookie時,我們應以如下方式為Set-Cookie或Set-Cookie2頭設一個值:

res.setHeader('Set-Cookie2', ..cookie value..);
/* Basic Server的核心模塊會創建一個HTTP服務器對象,附加Basic Server上用于檢查請求,然后給予適當響應的功能 Basic Server可以通過判斷Host頭部匹配的容器對象響應來自多個域名的請求 */var http = require('http');var url = require('url');exports.createServer = function(){  var htserver = http.createServer(function(req, res){    req.basicServer = {urlparsed: url.parse(req.url, true)};    processHeaders(req, res);    dispatchToContainer(htserver, req, res);  });  htserver.basicServer = {containers: []};  htserver.addContainer = function(host, path, module, options){    if (lookupContainer(htserver, host, path) != undefined){      throw new Error("Already mapped " + host + "/" + path);    }    htserver.basicServer.containers.push({host: host, path: path, module: module, options: options});    return this;  }  htserver.useFavIcon = function(host, path){    return this.addContainer(host, "/favicon.ico", require('./faviconHandler'), {iconPath: path});  }  htserver.docroot = function(host, path, rootPath){    return this.addContainer(host, path, require('./staticHandler'), {docroot: rootPath});  }  return htserver;}var lookupContainer = function(htserver, host, path){  for (var i = 0; i < htserver.basicServer.containers.length; i++){    var container = htserver.basicServer.containers[i];    var hostMatches = host.toLowerCase().match(container.host);    var pathMatches = path.match(container.path);    if (hostMatches !== null && pathMatches !== null){      return {container: container, host: hostMatches, path: pathMatches};    }  }  return undefined;}//用于搜索req.headers數組以查找cookie和host頭部,因為這兩個字段對請求的分派都很重要//這個函數在每一個HTTP請求到達時都會被調用//還有很多其他的HTTP頭部字段(Accept Accept-Encoding Accept-Language User-Agent)var processHeaders = function(req, res){  req.basicServer.cookies = [];  var keys = Object.keys(req.headers);  for (var i = 0; i < keys.length; i++){    var hname = keys[i];    var hval = req.headers[hname];    if (hname.toLowerCase() === "host"){      req.basicServer.host = hval;    }    //提取瀏覽器發送的cookie    if (hname.toLowerCase() === "cookie"){      req.basicServer.cookies.push(hval);    }  }}//查找匹配的容器,分派請求到對應的容器中//這個函數在每一個HTTP請求到達時都會被調用var dispatchToContainer = function(htserver, req, res){  var container = lookupContainer(htserver, req.basicServer.host, req.basicServer.urlparsed.pathname);  if (container !== undefined){    req.basicServer.hostMatches = container.host;    req.basicServer.pathMatches = container.path;    req.basicServer.container = container.container;    container.container.module.handle(req, res);  }else {    res.writeHead(404, {'Content-Type': 'text/plain'});    res.end("no handler found for " + req.basicServer.host + "/" + req.basicServer.urlparsed);  }}

staticHandler.js

//用于處理文件系統內的文件,docroot選項指被存放文件所在文件夾的路徑,讀取該目錄下的指定文件var fs = require('fs');var mime = require('mime');var sys = require('sys');exports.handle = function(req, res){  if (req.method !== "GET"){    res.writeHead(404, {'Content-Type': 'text/plain'});    res.end("invalid method " + req.method);  } else {    var fname = req.basicServer.container.options.docroot + req.basicServer.urlparsed.pathname;    if (fname.match(///$/)) fname += "index.html"; //如果URL以/結尾    fs.stat(fname, function(err, stats){      if (err){        res.writeHead(500, {'Content-Type': 'text/plain'});        res.end("file " + fname + " not found " + err);      } else {        fs.readFile(fname, function(err, buf){          if (err){            res.writeHead(500, {'Content-Type': 'text/plain'});            res.end("file " + fname + " not readable " + err);          } else {            res.writeHead(200, {'Content-Type': mime.lookup(fname),              'Content-Length': buf.length});            res.end(buf);          }        });      }    });  }}

faviconHandler.js

//這個處理函數處理對favicon.ico的請求//MIME模塊根據給出的圖標文件確定正確的MIME類型,網站圖標favicon可以是任何類型的圖片,但是我們必須要告訴瀏覽器是哪個類型//MIME模塊,用于生成正確的Content-Type頭var fs = require('fs');var mime = require('mime');exports.handle = function(req, res){  if (req.method !== "GET"){    res.writeHead(404, {'Content-Type': 'text/plain'});    res.end("invalid method " + req.method);  } else if (req.basicServer.container.options.iconPath !== undefined){    fs.readFile(req.basicServer.container.options.iconPath, function(err, buf){      if (err){        res.writeHead(500, {'Content-Type': 'text/plain'});        res.end(req.basicServer.container.options.iconPath + "not found");      } else {        res.writeHead(200, {'Content-Type': mime.lookup(req.basicServer.container.options.iconPath),        'Content-Length': buf.length});        res.end(buf);      }    });  } else {    res.writeHead(404, {'Content-Type': 'text/plain'});    res.end("no favicon");  }}

redirector.js

/* 把一個域的請求重定向到另一個上,例如將www.example.com重定向到example.com上,或者使用簡短的URL跳轉到較長的URL 實現這兩種情況,我們需要在HTTP響應中發送301(永久移除)或者302(臨時移除)狀態碼,并且指定location頭信息。有了這個組合 信號,web瀏覽器就知道要跳轉到另一個web位置了 *///地址http://localhost:3000/l/ex1 會跳轉到http://example1.comvar util = require('util');var code2url = {'ex1': 'http://example1.com', 'ex2': "http://example2.com"};var notFound = function(req, res){  res.writeHead(404, {'Content-Type': 'text/plain'});  res.end("no matching redirect code found for " + req.basicServer.host + "/" + req.basicServer.urlparsed.pathname);}exports.handle = function(req, res){  if (req.basicServer.pathMatches[1]){    var code = req.basicServer.pathMatches[1];    if (code2url[code]){      var url = code2url[code];      res.writeHead(302, {'Location': url});      res.end();    } else {      notFound(req, res);    }  } else {    notFound(req, res);  }}

docroot目錄下:有favicon.png

index.html

<html><head></head><body>  <h1>Index</h1>  <p>this is a index html.</p></body></html>

希望本文所述對大家nodejs程序設計有所幫助。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 永川市| 井冈山市| 凤冈县| 建宁县| 新乡县| 淅川县| 石屏县| 喜德县| 古丈县| 喀喇| 沙坪坝区| 德令哈市| 辽宁省| 佛冈县| 长武县| 定结县| 石楼县| 乌兰浩特市| 盐亭县| 昌邑市| 建湖县| 札达县| 始兴县| 观塘区| 绥中县| 文登市| 永川市| 东乡县| 贵阳市| 本溪市| 新平| 沙坪坝区| 精河县| 四子王旗| 松潘县| 洪江市| 论坛| 仁化县| 乐山市| 田东县| 福贡县|