使用nodejs不僅可以開發(fā)http、https服務(wù)器,而且還可以開發(fā)具有訪問網(wǎng)絡(luò)能力的客戶端,下面我貼出具體實現(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)
{
//獲取請求的pathname
var pathname=url.parse(req.url).pathname;
console.log("pathname:"+pathname);
var body="";//post正文內(nèi)容體
req.on("data",function(chunk)//監(jiān)聽讀取數(shù)據(jù)事件
{
body+=chunk;
});
req.on("end",function()
{
res.writeHead(200,{"Content-Type":"text/plain"});//寫入head參數(shù)
if(req.method=="GET")
{
//查詢參數(shù)(獲取到客戶端請求的參數(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ù)(獲取到客戶端請求的參數(shù))
var query=querystring.parse(body);
console.log("query:%j",query);
res.end("hello-server-post");
}
});
});
server.listen(1336,"localhost");
console.log("服務(wù)器啟動監(jiān)聽localhost:1336");
創(chuàng)建http服務(wù)器,并處理 GET和POST請求,拿到請求的路徑和參數(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:"中國",
abc:123,
});
//構(gòu)造http請求(客戶端)
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ā)送請求
Https使用
https服務(wù)器的創(chuàng)建,首先需要用到兩個文件(用于前后端數(shù)據(jù)傳輸時的加密):
"certificate.pem" //證書文件
"privatekey.pem" //私鑰文件
-
秘鑰和證書文件生成【如果你已經(jīng)有證書則可以跳過此步驟】
打開你的終端,【要確保安裝了openssl,安裝教程百度一下吧】依次執(zhí)行以下三條命令,最終會生成三個文件,分別是:
privatekey.pem
certrequest.csr
certificate.pem
/**
* ssl證書生成
* 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簽名證書】
*/
-
https服務(wù)器
有了我們需要的
"certificate.pem" //證書文件
"privatekey.pem" //私鑰文件
我們就可以創(chuàng)建并啟動一個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)聽localhost:443");
-
https客戶端
【注意】以下代碼會報 "Error: self signed certificate"錯誤,應(yīng)該證書是我們自己簽發(fā)的,是證書校驗失敗導(dǎo)致(如果自簽名證書沒問題可以噴我)正常情況下 我們發(fā)布版本需要到權(quán)威的SSL/TLS證書簽發(fā)機(jī)構(gòu)進(jìn)行簽發(fā)。
不過 你可以通過瀏覽器去驗證一下你的https服務(wù)器是否正常啟動并運行。
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請求(客戶端)【自簽名證書非法,應(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ā)送請求