HTTP核心模組

2022-08-29 17:12:28 字數 2586 閱讀 8781

let msg = "";

//構造響應報文

響應物件的常用方法

異常處理

請求會有很多很多,如果乙個請求出錯會導致伺服器掛掉,別的請求無法訪問伺服器.

所以再做伺服器開發的時候要保證如果乙個請求出錯,不影響其他請求

如何處理異常:

tmp = tmp.replace("$","列表");

res.write(tmp);

//結束請求

安裝xtemplate模板語法

使用

var xtpl = require('xtpl');

xtpl.renderfile('./x.xtpl',,function(error,content));

express的安裝

基本使用

**結構

"use strict";

const express = require("express");

res.setheader("content-type","text/html;charset=utf8");

res.write("我的天哪");

res.end();

}); console.log("開始監聽...");

});

res.setheader("content-type", "text/html;charset=utf-8");

res.write("歡迎");

res.end();

});res.write("");

res.end();

});

//請求位址的形式  

//解析url中的引數 方式1

//let query = url.parse(req.url, true).query;

//解析url中的引數 方式2

////過期的方法,不推薦使用

"id"));

res.write("ceshi");

res.end();

});

//請求位址的形式  

//獲取引數

console.log(req.params);

console.log(req.params.username);

res.send("haha");

});

//優先跟註冊位址註冊的順序有關

res.setheader("content-type", "text/html;charset=utf-8");

res.write("萬用字元哈

"); next();

});

"use strict";

const express = require("express");

let mainrouter = express.router();

mainrouter.get("/main/list", (req, res) => );

mainrouter.get("/main/haha", (req, res) => );

console.log("正在載入...");

});

Node核心模組Http

let realpath path.join dirname,pathname 拼接真實檔案的路徑 tryelse catch e server.listen 3000,function 複製 let http require http let url require url let path re...

http核心模組 node

ttp超文字傳輸協議 協議至少雙方 http雙方!客戶端 瀏覽器 伺服器 bs 原生應用 qq 伺服器 cs 就是資料如何傳輸 特點 一問一答 先有請求,後有響應 5大特點 輕便 簡單快速 支援客戶 伺服器模式 無連線 不為每乙個請求保持住鏈結 無狀態 伺服器不記得客戶端是誰 cookie 請求與響...

node核心 http模組

node作為伺服器更多的是web伺服器 首先 http是乙個協議。裡面有通訊機制,狀態碼一大堆亂七八糟的東西。自己寫猴年馬月都寫不出來,node通過這個http這個物件幫我們整合。直接用 伺服器物件 http.createsever 快速搭建乙個伺服器,整合好 當訪問localhost 8088 c...