node.js學(xué)習(xí)心得

@node.js學(xué)習(xí)心得TOC

#node.js是構(gòu)建在Chrome v8 引擎上的一個(gè)javascript 運(yùn)行環(huán)境

說到node.js首先是學(xué)會(huì)安裝鹦付,win+r cmd進(jìn)入dos命令窗口

//1初始化環(huán)境目錄

npm init

//2確認(rèn)版本信息點(diǎn)擊enter確認(rèn)直到is this ok 出現(xiàn)

npm init -y//默認(rèn)配置信息

npm install

//引用時(shí)

var fs=require('fs‘);

var http=require('require);

//創(chuàng)建服務(wù)器

http.creatSevers(function(req,res){

})

//讀取文件

fs.readFile('文件名','utf-8',function(err,data){

if(!err){

}

else{

}

})

//寫入信息

fs.writeFile('路徑',寫入數(shù)據(jù),回調(diào)函數(shù))厕氨;//在文件不存在時(shí)可以創(chuàng)建文件

//若想追加而不是覆蓋,加入?yún)?shù){flag:'a'}

//安裝淘寶鏡像

npm install -g cnpm -- registry=http://registry.npm .taobao.org

//安裝nodejs提示工具

npm install --save -dev@types/node

//解決跨域

res.writeHead(200,{'Content-Type':'text-html;charset=utf-8'})\

//向服務(wù)器發(fā)起請(qǐng)求

fetch(路徑汪诉,方法)//fetch()的返回值為promise對(duì)象所以經(jīng)常采用

fetch().then().then();

Express框架

//首先我們要安裝express

npm init

cnpm install express --save

用 get接受get請(qǐng)求post接受post請(qǐng)求鳄橘,res.send();//向客戶端返回?cái)?shù)據(jù)并結(jié)束

//express框架里采用路由分發(fā)機(jī)制,在一個(gè)js文件可以存在多個(gè)錄由

var express=require(‘express’);var app=express();

app.get(’/’,function(req,res){req.query能拿到get請(qǐng)求傳來的參數(shù)});app.post(’/’,function(req,res){req.body能接受post請(qǐng)求傳來的的參數(shù)});

POST請(qǐng)求在客戶端一定要帶上請(qǐng)求頭headers:{‘content-Type’:‘a(chǎn)pplication/x-www-form-url.encoded’}

統(tǒng)一目錄開發(fā) ,協(xié)同開發(fā)要使用腳手架組件工具express-generator;

安裝,npm install express-generator -g

使用,express -h(打出常見命令)-v(模板引擎) -c(設(shè)置CSS) dir查看當(dāng)前目錄所有文件

所有包 cnpm install

express --view =pug 文件名

bin目錄為啟動(dòng)目錄 node-modules為所有包,public里放前端靜態(tài)資源routes目錄存放錄由文件

var express=require(‘express’);

var router=express.Router();

//在錄由寫完后要導(dǎo)出module.exports=router;

下面就是我寫的登陸錄由

let express=require('express');

let router=express.Router();

let fs=require('fs');

let Person=require('../moudel/person');

router.post('/',function(req,res,next){

let loginer=req.body;

console.log(loginer.username+"第一次");

let obj1={};

fs.readFile('./data/myData.txt','utf-8',(err,data)=>{

? ? if(!err){

? ? ? ? console.log('讀取本地注冊(cè)信息成功第二次');

? ? ? ? let dat=JSON.parse(data);

? ? ? ? for(let i in dat){

? ? ? ? if(dat[i].username==loginer.username&&dat[i].pwd==loginer.pwd){

? ? ? ? ? ? obj1.msg='登陸成功';

? ? ? ? ? ? obj1.code = 204;

? ? ? ? }else if(dat[i].username!=loginer.username&&dat[i].pwd==loginer.pwd){

? ? ? ? ? ? obj1.msg = '該用戶不存在仅叫!';

? ? ? ? ? ? ? obj1.code = 204;

? ? ? ? }

? ? ? ? else if(dat[i].username==loginer.username&&dat[i].pwd!=loginer.pwd){

? ? ? ? ? ? obj1.msg = '密碼錯(cuò)誤帜篇!';

? ? ? ? ? ? obj1.code = 206;

? ? ? ? }

? ? }

? ? }

? else{

? ? console.log('讀取本地注冊(cè)信息失敗。诫咱。笙隙。。坎缭。竟痰。第三次');

? }

? console.log(obj1.code+"第三次");

? res.write(JSON.stringify(obj1.msg));

? res.end();

});

});

router.post('/regist',function(req,res,next){

let parmes=req.body;

console.log(parmes);

// fs.readFile('./data/myData.txt','utf-8',(err,data)=>{

//? ? req.body

// })

// console.log(parmes+"測(cè)試");

saveData(parmes,function(obj){

? ? res.write(JSON.stringify(obj.msg));//將數(shù)據(jù)反應(yīng)給前端

? ? res.end();

});

});

router.get('/bookKinds',function(req,res,next){

? ? let obj={};

? ? let obj1={};

? ? fs.readFile('./data/books.json','utf-8',(err,data)=>{

? ? ? ? if(!err){

? ? ? ? ? ? obj.data=JSON.parse(data);

? ? ? ? ? ? obj1.resultcode=obj.data.resultcode;

? ? ? ? ? ? obj1.reason=obj.data.reason;

? ? ? ? ? ? obj1.result=obj.data.result;

? ? ? ? ? ? res.write(JSON.stringify(obj1));

? ? ? ? ? ? res.end();

? ? ? ? }else{

? ? ? ? ? ? obj1.resultcode=0;

? ? ? ? ? ? obj1.reason="err";

? ? ? ? ? ? obj1.result="";

? ? ? ? ? ? res.write(JSON.stringify(obj1));

? ? ? ? ? ? res.end();

? ? ? ? }

? ? });

})

function saveData(parm,fn){

? ? let obj={};

? ? let username=parm.username;

? ? let pwd=parm.pwd;

? ? let tel=parm.tel;

? ? let sex=parm.sex;

? ? let person=new Person(username,pwd,tel,sex);

? ? getData(function(dataArr){

? ? ? ? dataArr.push(person);

? ? ? ? console.log(JSON.stringify(dataArr));

? ? ? ? console.log('------------------------------------');

? ? fs.writeFile('./data/myData.txt',JSON.stringify(dataArr),err=>{

? ? ? ? if(!err){

? ? ? ? ? ? obj.code=202;

? ? ? ? ? ? obj.msg="注冊(cè)成功";

? ? ? ? }

? ? ? ? else{

? ? ? ? ? ? obj.code=502;

? ? ? ? ? ? obj.msg="注冊(cè)失斍┕场!";

? ? ? ? }

? ? ? ? fn(obj);

? ? })

? ? });

}

function getData(fn){

? ? let arr=[];

? ? fs.readFile('./data/myData.txt','utf-8',(err,data)=>{

? ? ? ? if(!err){

? ? ? ? ? ? console.log('本地注冊(cè)信息',data);

? ? ? ? ? ? arr=JSON.parse(data);

? ? ? ? }

? ? ? ? else{

? ? ? ? ? ? console.log('讀取本地注冊(cè)信息失敗');

? ? ? ? }

? ? ? ? fn(arr);

? ? });

}

module.exports = router;

---------------------

<!DOCTYPE html>

<html lang="en">

<head>

? ? <meta charset="UTF-8">

? ? <meta name="viewport" content="width=device-width, initial-scale=1.0">

? ? <meta http-equiv="X-UA-Compatible" content="ie=edge">

? ? <link rel="stylesheet" >

? ? <title>書城</title>

? ? <style>

? ? *{padding:0;margin:0;}

? ? #box{

? ? ? ? box-sizing: border-box;

? ? ? ? width:300px;

? ? ? ? height:330px;

? ? ? ? border: 1px solid #cccccc;

? ? ? ? margin-left:50px;

? ? ? ? margin-top: 50px;

? ? ? ? list-style: none;


? ? ? position: relative;

? ? }

? ? #box li{

? ? ? ? box-sizing: border-box;

? ? ? ? width:300px;

? ? ? ? height:100px;

? ? ? ? line-height: 100px;

? ? ? ? padding-left: 20px;

? ? ? ? background-color: rgba(243, 52, 19, 0.8);

? ? ? ? border-bottom: 1px solid white;

? ? }

? ? #box li input{

? ? height:30px;? width:200px;

? ? border: 1px solid transparent;

? margin-left: 20px;}

? ? #box li label i{

? ? ? ? font-size: 30px;


? ? }

? ? #bt{

? ? ? ? width: 250px;

? ? ? ? height:35px;

? ? ? ? border: 1px solid transparent;

? ? ? ? margin-left:50%;

? ? ? ? margin-top:50px;

? ? ? ? transform: translate(-50%,-50%);

? ? ? ? background-color: rgb(247, 80, 14);

? ? ? ? color: white;

? ? ? ? font-size: 20px;

? ? }

? ? #box a{

? ? ? ? text-decoration: none;position: absolute;

? ? ? ? }

? ? .lose a{

? ? ? ? ? ? margin-top:20px;left: 20px;

? ? ? ? }

? ? ? ? .regist a{

? ? ? ? ? ? right: 20px; margin-top:20px;

? ? ? ? }

? ? </style>

</head>

<body>

? ? <ul id="box">

? ? ? ? <li> <label for=""><i class="iconfont icon-denglu2"></i></label><input type="text" placeholder="用戶名" class="uname"></li>

? ? ? ? <li><label for=""><i class="iconfont icon-mima"></i></label><input type="password" placeholder="密碼" class="upwd"></li>


? ? ? ? <button id="bt"> 登錄</button>

? ? ? ? ? <div class="lose">

? ? ? ? ? ? ? ? <a href="">忘記密碼</a>

? ? ? ? ? ? </div>

? ? ? ? <div class="regist">

? ? ? ? ? ? <a href="./regist.html">注冊(cè)新用戶</a>

? ? ? ? </div>

? </ul>

? <script>

? document.querySelector('#bt').onclick=function(){

let username=document.querySelector('.uname').value.trim();

let pwd=document.querySelector('.upwd').value.trim();

let url='http://127.0.0.1:3000';

let myUrl=`${url}/login`;

// 判斷輸入框的值

if(username===""||pwd===""){

? ? alert('輸入框不能為空');

}

fetch(myUrl,{

? ? method:"POST",

? ? body:`username=${username}&pwd=${pwd}`,

? ? headers:{

? ? ? ? ? ? ? 'Content-Type':"application/x-www-form-urlencoded"

? ? ? ? ? ? ? ? ? ? }

}).then(data=>data.json()).then(res=>{

? ? console.log(res);

? ? if(res=='登陸成功'){

? ? ? ? window.location.;

? ? }

})

}

? ? </script>


</body>

</html>

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末坏快,一起剝皮案震驚了整個(gè)濱河市铅檩,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌莽鸿,老刑警劉巖昧旨,帶你破解...
    沈念sama閱讀 219,589評(píng)論 6 508
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場(chǎng)離奇詭異祥得,居然都是意外死亡兔沃,警方通過查閱死者的電腦和手機(jī),發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,615評(píng)論 3 396
  • 文/潘曉璐 我一進(jìn)店門级及,熙熙樓的掌柜王于貴愁眉苦臉地迎上來乒疏,“玉大人,你說我怎么就攤上這事饮焦∨挛猓” “怎么了?”我有些...
    開封第一講書人閱讀 165,933評(píng)論 0 356
  • 文/不壞的土叔 我叫張陵追驴,是天一觀的道長(zhǎng)械哟。 經(jīng)常有香客問我,道長(zhǎng)殿雪,這世上最難降的妖魔是什么暇咆? 我笑而不...
    開封第一講書人閱讀 58,976評(píng)論 1 295
  • 正文 為了忘掉前任,我火速辦了婚禮丙曙,結(jié)果婚禮上爸业,老公的妹妹穿的比我還像新娘。我一直安慰自己亏镰,他們只是感情好扯旷,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,999評(píng)論 6 393
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著索抓,像睡著了一般钧忽。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上逼肯,一...
    開封第一講書人閱讀 51,775評(píng)論 1 307
  • 那天耸黑,我揣著相機(jī)與錄音,去河邊找鬼篮幢。 笑死大刊,一個(gè)胖子當(dāng)著我的面吹牛,可吹牛的內(nèi)容都是我干的三椿。 我是一名探鬼主播缺菌,決...
    沈念sama閱讀 40,474評(píng)論 3 420
  • 文/蒼蘭香墨 我猛地睜開眼葫辐,長(zhǎng)吁一口氣:“原來是場(chǎng)噩夢(mèng)啊……” “哼!你這毒婦竟也來了伴郁?” 一聲冷哼從身側(cè)響起耿战,我...
    開封第一講書人閱讀 39,359評(píng)論 0 276
  • 序言:老撾萬榮一對(duì)情侶失蹤,失蹤者是張志新(化名)和其女友劉穎蛾绎,沒想到半個(gè)月后昆箕,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 45,854評(píng)論 1 317
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡租冠,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 38,007評(píng)論 3 338
  • 正文 我和宋清朗相戀三年鹏倘,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片顽爹。...
    茶點(diǎn)故事閱讀 40,146評(píng)論 1 351
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡纤泵,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出镜粤,到底是詐尸還是另有隱情捏题,我是刑警寧澤,帶...
    沈念sama閱讀 35,826評(píng)論 5 346
  • 正文 年R本政府宣布肉渴,位于F島的核電站公荧,受9級(jí)特大地震影響,放射性物質(zhì)發(fā)生泄漏同规。R本人自食惡果不足惜循狰,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,484評(píng)論 3 331
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望券勺。 院中可真熱鬧绪钥,春花似錦、人聲如沸关炼。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,029評(píng)論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽儒拂。三九已至寸潦,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間社痛,已是汗流浹背见转。 一陣腳步聲響...
    開封第一講書人閱讀 33,153評(píng)論 1 272
  • 我被黑心中介騙來泰國(guó)打工, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留褥影,地道東北人。 一個(gè)月前我還...
    沈念sama閱讀 48,420評(píng)論 3 373
  • 正文 我出身青樓咏雌,卻偏偏與公主長(zhǎng)得像凡怎,于是被迫代替她去往敵國(guó)和親校焦。 傳聞我的和親對(duì)象是個(gè)殘疾皇子,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 45,107評(píng)論 2 356

推薦閱讀更多精彩內(nèi)容