1. 服務(wù)器端
創(chuàng)建服務(wù)器
//用于創(chuàng)建網(wǎng)站服務(wù)器模塊
const http = require('http');
//app對象就是網(wǎng)站服務(wù)器對象
const app = http.createServer();
app.on('request',(req,res) => {
res.end('<h2>hellow user</h2>');
});
app.listen(3000);
console.log('創(chuàng)建成功')
訪問url:localhost:3000
2. HTTP協(xié)議

微信截圖_20200123172529.png
3. 獲取請求地址
//用于創(chuàng)建網(wǎng)站服務(wù)器模塊
const http = require('http');
//app對象就是網(wǎng)站服務(wù)器對象
const app = http.createServer();
app.on('request',(req,res) => {
//獲取請求地址
//req.url
console.log(req.url);
if (req.url == '/index' || req.url == '/') {
res.end('welcome to homepage');
}else if (req.url == '/list') {
res.end('not found');
}
//req.method
//獲取請求方式
console.log(req.method);
res.end('<h2>hellow user</h2>');
});
app.listen(3000);
console.log('創(chuàng)建成功')
4. 請求報文信息
//請求報文信息
console.log(req.headers);
console.log(req.headers['accept'])
5. 響應(yīng)報文
res.writeHead(400);
//類型
res.writeHead(200,{
'content-type':'text/html'
});
//中文編碼
res.writeHead(200,{
'content-type':'text/html;charset=utf8'
});
6.get請求參數(shù)
//用于處理url地址
const url = require('url');
//1.要解析的url地址
//2.將查詢參數(shù)解析成對象形式
console.log(url.parse(req.url,true));
let { query, pathname } = url.parse(req.url,true);
console.log(query.name)
console.log(query.age)
if (pathname == '/index' || pathname == '/') {
res.end('<h1>welcome to homepage哈哈</h1>');
}else if (pathname == '/list') {
res.end('welcome to list page');
}else{
res.end('not found');
}
7.post請求參數(shù)
//用于創(chuàng)建網(wǎng)站服務(wù)器模塊
const http = require('http');
//app對象就是網(wǎng)站服務(wù)器對象
const app = http.createServer();
//處理請求參數(shù)模塊
const querystring = require('querystring');
app.on('request',(req,res) => {
//post參數(shù)是通過事件的方式接受的
//data 當請求參數(shù)傳遞的時候觸發(fā)data事件
//end 當參宿和傳遞完成的時候觸發(fā)end事件
let postParams = '';
req.on('data',parmas => {
postParams += parmas;
})
req.on('end',() => {
console.log(querystring.parse(postParams));
});
res.end('ok');
});
app.listen(3000);
console.log('創(chuàng)建成功')

微信截圖_20200123204736.png
8. 路由
//1.引入http
//2.創(chuàng)建網(wǎng)站服務(wù)器
//3.為網(wǎng)站服務(wù)器對象添加請求事件
//4.實現(xiàn)路由功能
//獲取客戶端的請求方式
const http = require('http');
const url = require('url');
const app = http.createServer();
app.on('request', (req,res) => {
const method = req.method.toLowerCase();
//獲取請求地址
const pathname = url.parse(req.url).pathname
res.writeHead(200,{
'content-type':'text/html;charset=utf8'
});
if(method == 'get'){
if (pathname == '/' || pathname == '/index'){
res.end('歡迎來到首頁');
}else if(pathname == '/list') {
res.end('歡迎來到列表頁')
}else{
res.end('您訪問的頁面不存在')
}
}else if (method =='post') {
}
});
app.listen(3000);
console.log('服務(wù)器啟動成功');
9.靜態(tài)
引入第三方模塊npm install mine
mime.getType根據(jù)資源路徑返回資源類型
const http = require('http');
const app = http.createServer();
const url = require('url');
const path = require('path');
const fs = require('fs');
const mime = require('mime');
app.on('request', (req,res) => {
//獲取用戶的請求路徑
let pathname = url.parse(req.url).pathname;
pathname=pathname == '/'?'/lunbo.html':pathname;
//將用戶的請求路徑轉(zhuǎn)換為實際的服務(wù)器硬盤路徑
let realpath = path.join(__dirname,'public' + pathname);
console.log(realpath);
let type = mime.getType(realpath);
// console.log(mime.getType(realpath));
//讀取文件
fs.readFile(realpath,(error,result) => {
//如果文件讀取失敗
if (error != null) {
res.writeHead(404,{
'content-type':'text/html;charset=utf8'
})
res.end('文件讀取失敗');
return;
}
res.writeHead(200,{
'centent-type':type
})
res.end(result)
});
});
app.listen(3000);
console.log('服務(wù)器啟動成功');