nodejs基礎運用——類比註冊登陸

來源:互聯網
上載者:User

標籤:外殼   json   node.js   連接埠號碼   read   json格式   自己的   瀏覽器   沒有   

正如維基百科 所說:“Node.js 是Google V8 引擎、libuv平台抽象層 以及主體使用 Javscript 編寫的核心庫三者集合的一個封裝外殼。” 除此之外,值得注意的是,Node.js 的作者瑞恩·達爾 (Ryan Dahl) 的目標是建立具有即時推送能力的網站。在 Node.js 中,他給了開發人員一個使用事件驅動來實現非同步開發的優秀解決方案。

今天就來分享一個基礎的nodejs運用----------類比註冊登陸。現在都是用的第三方,幾乎沒人用原生的來自己寫。不過如果你是初學者,還是可以看看的。希望大神門勿噴小女子哈!!!

首先,您需要建立一個檔案,在檔案下建一個html檔案夾,json檔案,,以下代碼中用到的檔案都在圖中。

此外,json資料格式如下:

直接在命令台,進入你的index.js使用命令node index.js 來在瀏覽器展示。提示使用你自己的網域名稱+連接埠號碼

 

 好了,直接上的代碼吧

//建立伺服器
var http=require(‘http‘);

var fs=require(‘fs‘);
var url=require(‘url‘);
var querystring=require(‘querystring‘);

var server=http.createServer(handle).listen(3000);
function handle(req,res){
var filePath=‘‘;
//顯示的頁面
if (req.url=="/"){
filePath=‘./html/index.html‘;
fs.exists(filePath,function(exists){
if (exists){
fs.readFile(filePath,function(err,data){
if (err){return}
res.end(data);
})
}else{
send404(res)
}
})
}
//通過req.method判斷使用者是否註冊和登陸
// get 判斷使用者是否註冊過 post 沒有註冊的註冊,並儲存響應資料到資料json表中
else if(req.url.indexOf(‘/from‘)!=-1){
if (req.method==‘GET‘){
// console.log(req.url) ///from?username=14545&password=4546536636
// .解析url,返回一個json格式的數組 url.parse(‘http://www.baidu.com?page=1’,true);
var parme=url.parse(req.url,true);
// console.log(parme)
// {
// protocol: null,
// slashes: null,
// auth: null,
// host: null,
// port: null,
// hostname: null,
// hash: null,
// search: ‘?username=14545&password=4546536636‘,
// query: { username: ‘14545‘, password: ‘4546536636‘ },
// pathname: ‘/from‘,
// path: ‘/from?username=14545&password=4546536636‘,
// href: ‘/from?username=14545&password=4546536636‘ }
var info=parme.query;
// 拿到使用者輸入的資訊,與資料庫對比
var username=info.username;
var password=info.password;
fs.readFile(‘./tsconfig.json‘,‘utf-8‘,function(err,data){
if (err){
return;
}
// console.log(data)
// {
// "success":‘1‘,
// "data": [
// {"username":"wj",
// "pasword":"123456"
// }
// ]
// }
// 資料轉化
var data=JSON.parse(data);
// console.log(data)
// { success: ‘1‘,
// data: [ { username: ‘wj‘, pasword: ‘123456‘ } ] }
var message=data.data;
for(var i=0;i<message.length;i++){
if (message[i].username==username&&message[i].password==password){
// 有此賬戶,可直接登入
filePath=‘./html/login.html‘;
break;
}else{
// 沒有此賬戶,需要註冊
filePath=‘./html/reg.html‘
}
}

fs.exists(filePath,function(exists){
if (exists){
fs.readFile(filePath,function(err,data){
if (err){return}
res.end(data);
})
}else{
send404(res)
}
})


})
}
// POST傳輸資料
else if(req.method==‘POST‘){
var postData=‘‘;
filePath=‘./html/index.html‘;
req.on(‘data‘,function(chunk){
postData+=chunk;
// console.log(postData)
})
req.on(‘end‘,function(){
var data=querystring.parse(postData);
var obj={};
obj.username=data.username;
obj.password=data.password;
fs.readFile(‘./tsconfig.json‘,‘utf-8‘,function(err,data){
if (err){
return;
}
var data=JSON.parse(data);
data.data.push(obj)
fs.writeFile(‘./tsconfig.json‘,JSON.stringify(data),function(){
})
})
})

fs.exists(filePath,function(exists){
if (exists){
fs.readFile(filePath,function(err,data){
if (err){return}
res.end(data);
})
}else{
send404(res)
}
})
}
}
//顯示的頁面
else{
filePath=‘.‘+req.url;
// console.log(req.url);
// console.log(filePath);
fs.exists(filePath,function(exists){
if (exists){
fs.readFile(filePath,function(err,data){
if (err){return}
res.end(data);
})
}else{
send404(res)
}
})

}
}

//整過過程都是非同步,所以判斷檔案的函數需要寫在內部
function send404(res){
fs.readFile(‘./html/404.html‘,function(err,data){
if (err){return}
res.end(data)
})
}

nodejs基礎運用——類比註冊登陸

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.