HTTP模塊
第一個(gè)板塊:簡單介紹HTTP模塊用法
//第一步:引入模塊http模塊???是核心模塊
var http=require('http')
//第二步:創(chuàng)建http對象
var svr=http.createServer(function(){
//第四步:當(dāng)來請求的時(shí)候我所要執(zhí)行的任務(wù),在createServer中加上回調(diào),這個(gè)回調(diào)函數(shù)就是我所要執(zhí)行的任務(wù),createServer的作用就是當(dāng)有請求的時(shí)候,執(zhí)行function這個(gè)里面的代碼
});
//第三步:監(jiān)聽端口
svr.listen(3001)
第二個(gè)板塊:介紹請求對象和響應(yīng)對象
//第一步:引入模塊http模塊???是核心模塊
var http=require('http')
//第二步:創(chuàng)建http對象
var svr=http.createServer(function(req,res){
res.write('hellow,hug')
res.end()//比搜啊是寫完
//現(xiàn)在只說回調(diào)函數(shù)中的參數(shù),及function中的參數(shù)
//console.log(a)//通過打印我們知道a,b是一個(gè)對象,而且是http模塊官方為我們寫好的對象???既然我們證明a,b是官方寫好的對象,我就可以告訴大家
//a(function中的第一個(gè)參數(shù)表示請求對象)一般情況下我們用req代表請求對象request ? ?是繼承自一個(gè)讀流
//b(function中的第二個(gè)參數(shù)表示響應(yīng)對象)一般情況下我們用res代表響應(yīng)對象response???是繼承自一個(gè)寫流
});
svr.listen(3001)
第三個(gè)板塊:發(fā)送index.html文件
var http=require('http')
var fs=require(''fs);
var svr=http.createServer(function(req,res){
//通過fs讀網(wǎng)頁
fs.readFile('文件路徑',function(err,data){
res.write(data)
res.end()
})
});
svr.listen(3001)
第四個(gè)板塊:請求對象的兩個(gè)方法url ?method
var http=require('http')
var fs=require('fs');
var url=require('url')
var qs=require('querystring')
var svr=http.createServer(function(req,res){
//當(dāng)點(diǎn)擊提交的時(shí)候,我來解析出GET提交的數(shù)據(jù)
if(req.url!=='/'){
var up=url.parse(req.url)
var qsq=qs.parse(up.query)
console.log(qsq.user)
}
//既然req對象故req就要有什么能干什么
//1、url????表示請求的url地址???console.log(req.url)??打印出'/',故當(dāng)前的url就是'/'
//2、method http請求方法post??get delete put update Head
//console.log(reg.method)
fs.readFile('文件路徑',function(err,data){
res.write(data)
res.end()
})
});
svr.listen(3001)
第五版塊:解析GET請求
var http=require('http');
var url=require('url')
var qs=require('querystring')
var fs=require('fs');
var svr=http.createServer(function(req,res){
//想判斷表單提交還是網(wǎng)址請求
//如果是正常網(wǎng)址請求,則直接發(fā)送index.html文件
fs.readFile('文件路徑',function(err,data){
res.write(data)
res.end()
})
//如果有GET請求??普通請求是'/'??get請求'/?'
var up=url.parse(req.url).query
if(up!==null){
qs.parse(up).user
}
})
svr.listen(3001)
//4、使用http模塊顯示兩次頁面 ?頁面跳轉(zhuǎn)
//1>在第一個(gè)頁面中鏈接、點(diǎn)擊鏈接進(jìn)入第二個(gè)頁面
//2>提示:需要兩個(gè)HTML頁面
var http=require('http')
var fs=require('fs')
var svr=http.createSrver(function(req,res){
fs.readFile('文件路徑',function(err,data){//一般有事件就沒有err,其他都有
res.write(data)
res.end()
})
fs.readFile('第二個(gè)頁面',function(err,data){
res.write(data)
res.end()
})
})
svr.listen(3001)
post表單提交
//js代碼
var http=require('http');
var fs=equire('fs');
http.createServer(function(req,res){
if(req.method=="POST"){
//因?yàn)閞eq是請求對象,且繼承自讀流??data??end倆事件??//如何使用解析post數(shù)據(jù)
req.on('data',function(chunck){
console.log(chunck.toString())
})
}else{
fs.readFile('HTML路徑',function(err,data){
res.write(data)
res.end()
})
}
}).listen(3001)
//html代碼
//響應(yīng)favicon
var http=require('http');
http.createServer(function(req,res){
if(req.url==='/favicon.ico'){//當(dāng)請求favion.ico時(shí),返回響應(yīng)圖片
//favicon.ico瀏覽器自動請求,顯示在瀏覽器標(biāo)簽中的圖片
//故favicon.ico是一個(gè)ico格式的圖片??png jpg jepg
fs.readFile('圖片路徑',function(err,data){
//res.setHeader('content-type','image-icon')
res.write(data)
res.end()
})
}else{
fs.readFile('html路徑',function(err,data){
res.write(data)
res.end()
})
})
//只有發(fā)送了html文件才會請求favicon
http的登錄注冊
js代碼
var http=require('http')
var fs=require('fs')
var qs=require('querystring')
http.createServer(function(req,res){
if(req.url==='/'&&req.method==='GET'){
fs.readFile('總頁面',functio(err,data){
res.write(data)
res.end()
})
}else if(req.url==='/denglu'&& req.method==='GET'){
fs.readFile('登錄頁面',function(err,data){
res.write(data)
res.end()
})
}else if(req.url==='/zhuce' && req.method==='POST'){
fs.readFile('注冊面',functio(err,data){
req.on('data',function(data){
fs.appendFile('./userDatabase',data.toString(),function(){
res.write('注冊成功')
res.end()
})
})
}else if(req.url==='/denglu'&&req.method==='POST'){
req.on('data',function(data){
//從數(shù)據(jù)庫中讀取注冊的內(nèi)容,然后和注冊的數(shù)據(jù)進(jìn)行匹配
fs.readFile('自動創(chuàng)建的內(nèi)容',function(err,data){
})
})
}
//if(req.method=='POST'){
//req.on('data',function(data){
//fs.appendFile('./userDatabase',data.toString(),function(){
//res.write('注冊成功')
//res.end()
//})
//})
//}else{
//fs.readFile('注冊頁面html',function(err,data){
//res.write(data)
//res.end()
//})
//}
}).listen(3001)
注冊頁面html
登錄頁面html
登錄注冊頁面
http協(xié)議:
請求部分:請求行、請求頭、請求體
響應(yīng)部分:狀態(tài)碼、響應(yīng)頭、響應(yīng)體
post和get的區(qū)別:
1、post安全性高 ?get安全性低
2、post比get的傳輸量大
3、get追加到url后面,post不會追加到url后面