使用nodejs不僅可以開(kāi)發(fā)http落君、https服務(wù)器额各,而且還可以開(kāi)發(fā)具有訪問(wèn)網(wǎng)絡(luò)能力的客戶端褥芒,下面我貼出具體實(shí)現(xiàn)步驟垃喊。
Http的使用
- http服務(wù)器
/*http & https*/
var http=require("http");
var https=require("https");
var url=require("url");
var fs=require("fs");
var querystring=require("querystring");
//http-server端
var server=http.createServer(function(req,res)
{
//獲取請(qǐng)求的pathname
var pathname=url.parse(req.url).pathname;
console.log("pathname:"+pathname);
var body="";//post正文內(nèi)容體
req.on("data",function(chunk)//監(jiān)聽(tīng)讀取數(shù)據(jù)事件
{
body+=chunk;
});
req.on("end",function()
{
res.writeHead(200,{"Content-Type":"text/plain"});//寫(xiě)入head參數(shù)
if(req.method=="GET")
{
//查詢參數(shù)(獲取到客戶端請(qǐng)求的參數(shù))
var query=querystring.parse(url.parse(req.url).query);
console.log("query:%j",query);
res.end("hello-server-get");
}else if(req.method=="POST")
{
//查詢參數(shù)(獲取到客戶端請(qǐng)求的參數(shù))
var query=querystring.parse(body);
console.log("query:%j",query);
res.end("hello-server-post");
}
});
});
server.listen(1336,"localhost");
console.log("服務(wù)器啟動(dòng)監(jiān)聽(tīng)localhost:1336");
創(chuàng)建http服務(wù)器猾普,并處理 GET和POST請(qǐng)求,拿到請(qǐng)求的路徑和參數(shù)本谜,返回結(jié)果到客戶端初家。
- http客戶端
var http=require("http");
var https=require("https");
var fs=require("fs");
var querystring=require("querystring");
var options={
hostname:"localhost",
port:1336,
path:"/info/child?abc=123&name=China",
// method:"GET"
method:"POST"
};
//post內(nèi)容
var contents=querystring.stringify({
name:"中國(guó)",
abc:123,
});
//構(gòu)造http請(qǐng)求(客戶端)
var req=http.request(options,function(res)
{
console.log("STATUS:"+res.statusCode);//服務(wù)器返回的狀態(tài)碼
console.log("HEADERS:%j",res.headers);//服務(wù)器返回的響應(yīng)頭參數(shù)
res.setEncoding("utf8");
res.on("data",function(chunk)//服務(wù)器返回的數(shù)據(jù)
{
console.log("response:"+chunk);
});
});
req.write(contents);//post發(fā)送數(shù)據(jù)
req.end();//發(fā)送請(qǐng)求
Https使用
https服務(wù)器的創(chuàng)建,首先需要用到兩個(gè)文件(用于前后端數(shù)據(jù)傳輸時(shí)的加密):
"certificate.pem" //證書(shū)文件
"privatekey.pem" //私鑰文件
-
秘鑰和證書(shū)文件生成【如果你已經(jīng)有證書(shū)則可以跳過(guò)此步驟】
打開(kāi)你的終端丛肮,【要確保安裝了openssl另锋,安裝教程百度一下吧】依次執(zhí)行以下三條命令,最終會(huì)生成三個(gè)文件锨亏,分別是:
privatekey.pem
certrequest.csr
certificate.pem
/**
* ssl證書(shū)生成
* 1.執(zhí)行 openssl genrsa -out privatekey.pem 1024 【生成privatekey.pem私鑰】
* 2.執(zhí)行 openssl req -new -key privatekey.pem -out certrequest.csr 【生成certrequest.csr簽名】
* 3.執(zhí)行 openssl x509 -req -in certrequest.csr -signkey privatekey.pem -out certificate.pem 【生成certificate.pem簽名證書(shū)】
*/
-
https服務(wù)器
有了我們需要的
"certificate.pem" //證書(shū)文件
"privatekey.pem" //私鑰文件
我們就可以創(chuàng)建并啟動(dòng)一個(gè)https服務(wù)器了:
var http=require("http");
var https=require("https");
var url=require("url");
var fs=require("fs");
var querystring=require("querystring");
var options={
key: fs.readFileSync('./privatekey.pem'),
cert: fs.readFileSync('./certificate.pem'),
passphrase:"****"http://你的密碼
};
var server=https.createServer(options,function(req,res)
{
var pathname=url.parse(req.url).pathname;
console.log("pathname:"+pathname);
res.writeHead(200,{"Content-Type":"text/plain"});
res.end("hello-server");
});
server.listen(443,"localhost");
console.log("服務(wù)器監(jiān)聽(tīng)localhost:443");
-
https客戶端
【注意】以下代碼會(huì)報(bào) "Error: self signed certificate"錯(cuò)誤炕泳,應(yīng)該證書(shū)是我們自己簽發(fā)的纵诞,是證書(shū)校驗(yàn)失敗導(dǎo)致(如果自簽名證書(shū)沒(méi)問(wèn)題可以噴我)正常情況下 我們發(fā)布版本需要到權(quán)威的SSL/TLS證書(shū)簽發(fā)機(jī)構(gòu)進(jìn)行簽發(fā)。
不過(guò) 你可以通過(guò)瀏覽器去驗(yàn)證一下你的https服務(wù)器是否正常啟動(dòng)并運(yùn)行培遵。
https://localhost:443/
var options={
hostname:"localhost",
port:443,
path:"/info?abc=123&name=China",
method:"GET"
};
options.agent = new https.Agent(options);
//構(gòu)造https請(qǐng)求(客戶端)【自簽名證書(shū)非法浙芙,應(yīng)該使用正式正式】
var req=https.request(options,function(res)
{
console.log("STATUS:"+res.statusCode);
console.log("HEADERS:%j",res.headers);
res.setEncoding("utf8");
res.on("data",function(chunk)
{
console.log("response:"+chunk);
});
});
req.end();//發(fā)送請(qǐng)求