рдЕрднреНрдпрд╛рд╕ рд╕реЗ рдЕрдВрдд рддрдХ Node.js рд╕реАрдЦрдирд╛ред рднрд╛рдЧ 1

рдкреНрд░рд╛рдЧрд┐рддрд┐рд╣рд╛рд╕


Node.js рдореЙрдбреНрдпреВрд▓ рдФрд░ рдХрд┐рд╕реА рднреА рддрд░рд╣ рдХреЗ рддреИрдпрд╛рд░ рдХрд┐рдП рдЧрдП рд╕рдорд╛рдзрд╛рдиреЛрдВ рдкрд░ рдмрд╣реБрдд рд╕рд╛рд░реЗ рдкреНрд░рд▓реЗрдЦрди рд╣реИрдВ, рд▓реЗрдХрд┐рди рдЬрдм рдЖрдк рдХрд┐рд╕реА рд╕рд╛рдЗрдЯ рдХреЛ рд▓рд┐рдЦрдирд╛ рд╢реБрд░реВ рдХрд░рддреЗ рд╣реИрдВ, рддреЛ рдЖрдк рд╕рдорд╕реНрдпрд╛ рдореЗрдВ рднрд╛рдЧ рд▓реЗрддреЗ рд╣реИрдВ: "рдХрд╣рд╛рдВ рд╕реЗ рд╢реБрд░реВ рдХрд░реЗрдВ?"ред рдореИрдВ рдЖрдкрдХреЛ рд╡реНрдпрд╡рд╣рд╛рд░ рдореЗрдВ Node.js рд╕реАрдЦрдиреЗ рдХрд╛ рдЕрдкрдирд╛ рдЕрдиреБрднрд╡ рдмрддрд╛рдирд╛ рдЪрд╛рд╣рддрд╛ рд╣реВрдВред рдХрд╛рд░реНрдп рдХрд╛рдлреА рд╕рд░рд▓ рдФрд░ рд╕реАрдзрд╛ рд╣реИ - рдПрдХ рдЗрдВрдЯрд░рдиреЗрдЯ рд╕реЗрд╡рд╛ рдХреЗ рд╕рд╛рде рдЬреАрдкреАрдПрд╕ рдЯреНрд░реИрдХрд░ рдЬреЛ рд╣рдорд╛рд░реЗ рдЯреНрд░рд╛рдВрд╕рдореАрдЯрд░реЛрдВ рдХреЛ рдПрдХ рдирдХреНрд╢реЗ рдкрд░ рдкреНрд░рджрд░реНрд╢рд┐рдд рдХрд░рддрд╛ рд╣реИ, рдПрдХ рдпрд╛рддреНрд░рд╛ рдорд╛рд░реНрдЧ, рдЖрджрд┐ рдХреЛ рдЦреАрдВрдЪрддрд╛ рд╣реИ, рдХрд▓реНрдкрдирд╛ рдХрд┐рддрдиреА рджреЗрд░ рддрдХ рд▓реЗ рдЬрд╛рддреА рд╣реИред рдпрд╣ рдкрд░рд┐рдпреЛрдЬрдирд╛ рд╡реНрдпрд╛рд╡рд╕рд╛рдпрд┐рдХ рдирд╣реАрдВ рд╣реИ рдФрд░ рдЕрдкрдиреЗ рд▓рд┐рдП рдорд╛рдирд╡ рдЬрд╛рддрд┐ рдХреЗ рд▓рд╛рдн рдХреЗ рд▓рд┐рдП рд▓рд┐рдЦреА рдЧрдИ рд╣реИред

рд╣рдо рдХрд╛рд░реНрдпрд╕реНрдерд▓ рдХреЛ рд╕реБрд╕рдЬреНрдЬрд┐рдд рдХрд░рддреЗ рд╣реИрдВ


рдореБрдЭреЗ рдХрд╛рдо рдФрд░ рдШрд░ рджреЛрдиреЛрдВ рдЬрдЧрд╣ рдХрд╛рдо рдХрд░рдирд╛ рд╣реИ, рдХрд╛рдо рдХрд┐рд╕реА рднреА рддрд░рд╣ рд╕реЗ рд╕рд╛рдЗрдЯ рдмрд┐рд▓реНрдбрд┐рдВрдЧ рд╕реЗ рдЬреБрдбрд╝рд╛ рдирд╣реАрдВ рд╣реИ рдФрд░ рдЗрд╕реЗ рд╡рд░реНрдХрдлрд╝реНрд▓реЛ рдореЗрдВ рд╣рд╕реНрддрдХреНрд╖реЗрдк рдирд╣реАрдВ рдХрд░рдирд╛ рдЪрд╛рд╣рд┐рдПред рдЗрд╕рд▓рд┐рдП, рдПрдХ рдЖрдИрдбреАрдИ рдЪреБрдирдиреЗ рдкрд░, рд╡рд┐рдХрд▓реНрдк рдХреНрд▓рд╛рдЙрдб 9 рдЖрдИрдбреАрдИ рдкрд░ рдЧрд┐рд░ рдЧрдпрд╛ред рд╕реБрд╡рд┐рдзрд╛ рдХреЗ рд▓рд┐рдП, рд╡реЗрдмрд╕реНрдЯреЛрд░ рдХрд╛ рдЙрдкрдпреЛрдЧ рдШрд░ рдкрд░ рдХрд┐рдпрд╛ рдЧрдпрд╛ рдерд╛ ред рд╕рднреА рд╕рд╛рдЗрдЯ рдбреЗрдЯрд╛ рдХреЛ рдХрд╣реАрдВ рд╕рдВрдЧреНрд░рд╣реАрдд рдХрд░рдиреЗ рдХреА рдЖрд╡рд╢реНрдпрдХрддрд╛ рд╣реИ, рд╕реИрджреНрдзрд╛рдВрддрд┐рдХ рднрд╛рдЧ рдХрд╛ рдЕрдзреНрдпрдпрди рдХрд░рдиреЗ рдХреЗ рдмрд╛рдж, рдЗрд╕ рдкреНрд░рдХрд╛рд░ рдХреЗ рдбреАрдмреАрдПрдордПрд╕ рд╕реЗ рдкрд░рд┐рдЪрд┐рдд рд╣реЛрдиреЗ рдХрд╛ рдЕрднреНрдпрд╛рд╕ рдХрд░рдиреЗ рдХрд╛ рдирд┐рд░реНрдгрдп рд▓рд┐рдпрд╛ рдЧрдпрд╛ рдерд╛ред рдХрд╛рд░реНрдпрд╕реНрдерд▓ рд╕реЗ рдЬреБрдбрд╝реЗ рдирд╣реАрдВ рд╣реЛрдиреЗ рдХреЗ рд▓рд┐рдП, MongoDB рдФрд░ mongohq.com рдореЗрдВ рдЖрдзрд╛рд░ рдХреЗ рд▓рд┐рдП рдореБрдлреНрдд рд╣реЛрд╕реНрдЯрд┐рдВрдЧ рдХреЛ DBMS рдХреЗ рд░реВрдк рдореЗрдВ рдЪреБрдирд╛ рдЧрдпрд╛ рдерд╛ред
рдЗрд╕рд▓рд┐рдП, рд╣рдорд╛рд░реЗ рдкрд╛рд╕ рдПрдХ рдЦрд╛рд▓реА рдкрд░рд┐рдпреЛрдЬрдирд╛ рдФрд░ рдПрдХ рдЦрд╛рд▓реА рдЖрдзрд╛рд░ рд╣реИред рдЖрдк рдЖрдЧреЗ рдмрдврд╝ рд╕рдХрддреЗ рд╣реИрдВред
1. рд╡реЗрдм рдПрдкреНрд▓рд┐рдХреЗрд╢рди рдлреНрд░реЗрдорд╡рд░реНрдХ, рд╕рдмрд╕реЗ рдЖрдо рдореЗрдВ рд╕реЗ рдПрдХ рдПрдХреНрд╕рдкреНрд░реЗрд╕ рд╣реИ ред
2. рд╕рд╛рдЗрдЯ рдХреЛ рдкреВрд░реА рддрд░рд╣ рд╕реЗ рдПрдЪрдЯреАрдПрдордПрд▓ 5 рдореЗрдВ рд▓рд┐рдЦрдиреЗ рдХрд╛ рдирд┐рд░реНрдгрдп рд▓рд┐рдпрд╛ рдЧрдпрд╛ рдерд╛, рдЗрд╕рд▓рд┐рдП рдИрдЬреЗрдПрд╕ рдХреЛ рдЯреЗрдореНрдкрд▓реЗрдЯ рдЗрдВрдЬрди рдХреЗ рд░реВрдк рдореЗрдВ рдЪреБрдирд╛ рдЧрдпрд╛ рдерд╛ред
3. MongoDB рдбреНрд░рд╛рдЗрд╡рд░, рдЙрдирдореЗрдВ рд╕реЗ рдПрдХ рд╕рдВрдЦреНрдпрд╛ рд╣реИ, рд▓реЗрдХрд┐рди рдореЗрд░реА рдкрд╕рдВрдж mongodb рдереА ред
4. рдЙрдкрдпреЛрдЧрдХрд░реНрддрд╛ рдЗрдирдкреБрдЯ, рдиреЛрдб-рд╕рддреНрдпрд╛рдкрдирдХрд░реНрддрд╛ рдХрд╛ рд╕рддреНрдпрд╛рдкрдиред

рд╕рд╛рдЗрдЯ рд╕рдВрд░рдЪрдирд╛


 BigBrother - \
       рдирд┐рдпрдВрддреНрд░рдХ (рдбреЗрдЯрд╛рдмреЗрд╕ рдХреЗ рд╕рд╛рде рдХрд╛рдо рдХрд░рдиреЗ рдХреЗ рд▓рд┐рдП рд╕рднреА рдСрдмреНрдЬреЗрдХреНрдЯ)
       - рд╕рд╛рд░реНрд╡рдЬрдирд┐рдХ - \ (рд╕рд╛рдВрдЦреНрдпрд┐рдХреАрдп рдлрд╝рд╛рдЗрд▓реЗрдВ (рд╕реАрдПрд╕рдПрд╕, рдЬреЗрдПрд╕, рдЪрд┐рддреНрд░))
             - рд╕реА.рдПрд╕.рдПрд╕.
             - рдЬреЗ.рдПрд╕.
             - рдЪрд┐рддреНрд░
       - рдорд╛рд░реНрдЧ (рд╕рд╛рдЗрдЯ рдорд╛рд░реНрдЧ)
       - рджреГрд╢реНрдп (рдкреЗрдЬ рдЯреЗрдореНрдкреНрд▓реЗрдЯ)
       - config.js (рд╕реЗрдЯрд┐рдВрдЧреНрд╕, рдЙрджрд╛рд╣рд░рдг рдХреЗ рд▓рд┐рдП, рдбреЗрдЯрд╛рдмреЗрд╕ рдХрдиреЗрдХреНрд╢рди)
       - server.js (рд╕реНрд╡рдпрдВ рд╕рд░реНрд╡рд░)

рдкрд╣рд▓рд╛ рдХрджрдо: рд▓реЙрдЧ рдЗрди рдХрд░реЗрдВ


рд╡рд┐рд╖рдп рдХрд╛ рдЙрдкрдпреЛрдЧ рдЕрдзреНрдпрдпрди рд╕рд╛рдордЧреНрд░реА рдХреЗ рд░реВрдк рдореЗрдВ рдХрд┐рдпрд╛ рдЧрдпрд╛ рдерд╛ рдФрд░ рдХрд╛рд░реНрдп рд╡рд┐рд╢рд┐рд╖реНрдЯ рдерд╛, рдЙрдкрдпреЛрдЧрдХрд░реНрддрд╛рдУрдВ рдХреЛ рдкрдВрдЬреАрдХрд░рдг рдХрд░рдиреЗ, рдкрд╛рд╕рд╡рд░реНрдб рдкреБрдирд░реНрдкреНрд░рд╛рдкреНрдд рдХрд░рдиреЗ, рд╕рд╛рдЗрдЯ рдкрд░ рд▓реЙрдЧрд┐рди рдпрд╛рдж рд░рдЦрдиреЗ, рдореЗрд▓ рдХреА рдирд┐рдЧрд░рд╛рдиреА рдХрд░рдиреЗ рдФрд░ рд╕реНрд╡рдпрдВ рд▓реЙрдЧ рдЗрди рдХрд░рдиреЗ рдХреЗ рд▓рд┐рдП рд╕рдХреНрд╖рдо рдХрд░рдиреЗ рдХреЗ рд▓рд┐рдПред

1. рд╕рд╛рдЗрдЯ рдХрд╛ рдЪреЗрд╣рд░рд╛

рдЖрдЗрдбрд┐рдпрд╛: рд╕реНрдХреНрд░реАрди рдХреЗ рдХреЛрдиреЗ рдореЗрдВ, рдмрдЯрди рдкрд░ рдХреНрд▓рд┐рдХ рдХрд░реЗрдВ, рджрд░реНрдЬ рдХреА рдЧрдИ рдбреЗрдЯрд╛ рдХреА рдЖрдВрд╢рд┐рдХ рдЬрд╛рдБрдЪ рдХреЗ рд╕рд╛рде рд╕рд╛рдЗрдЯ рдореЗрдВ рдкреНрд░рд╡реЗрд╢ рдХрд░рдиреЗ / рджрд░реНрдЬ рдХрд░рдиреЗ рдХреА рдХреНрд╖рдорддрд╛ рдХреЗ рд╕рд╛рде рдПрдХ рдореЛрдбрд▓ рд╡рд┐рдВрдбреЛ рджрд┐рдЦрд╛рдИ рджреЗрддреА рд╣реИ (рднрд╡рд┐рд╖реНрдп рдореЗрдВ, рдпрд╣ рдЪреЗрдХ рдХреЛ рдЬрдЯрд┐рд▓ рдХрд░ рд╕рдХрддреА рд╣реИ)ред
рд╢реИрд▓реА рдкрддреНрд░рдХ рд╢реИрд▓реА
@import url(http://fonts.googleapis.com/css?family=Tenor+Sans&subset=latin,cyrillic); body{ font-family: 'Tenor Sans', sans-serif; } #mainmap{ width : 100%; } #topmenu{ width: 100%; height: 80px; background-color: white; } #topmenu #user{ background-color: rgb(228, 228, 228); cursor: pointer; position: absolute; top: 20px; right: 20px; vertical-align: middle; text-align: center; padding: 10px; border: 1px solid gray; } #topmenu #user:hover{ background-color: rgb(188, 188, 188); border: 1px solid gray; } .window{ position: fixed; top: 0px; left: 0px; width: 100%; height: 100%; background-color: rgba(40,40,40,0.5); z-index: 9999; color: rgb(80,80,80); display: none; } .window .back{ position: absolute; top: 0px; left: 0px; width: 100%; height: 100%; z-index: 0; } .window .wrap{ position: fixed; width: 500px; height: 400px; top: 50%; left: 50%; margin: -200px -250px; background-color: white; border: 1px solid silver; padding: 10px; z-index: 1; } .window .wrap .header{ font-size: 25px; color: rgb(40,40,40); width: 100%; border-bottom: 1px solid gray; padding-bottom: 5px; } .window .wrap .header .active{ color: rgb(40,90,40); background-color: rgb(220,200,200); } .window .wrap .header div{ display: inline; cursor: pointer; background-color: rgb(230,240,240); padding: 2px; } .window .wrap .header div:hover{ color: rgb(40,40,90); background-color: rgb(230,230,230); } .window .wrap .msg{ display: none; position: absolute; top: 41px; left: 10px; width: 480px; background-color: rgb(220,100,100); padding: 10px; color: black; } .window .wrap .line{ margin-top: 10px; margin-left: 50px; } .window .wrap .line .label{ font-size: 20px; } .window .wrap .line .edit input[type='text'], .window .wrap .line .edit input[type='email'], .window .wrap .line .edit input[type='password']{ width: 400px; height: 25px; margin-top: 5px; border: 1px solid silver; font-size: 20px; } .window .wrap .line .edit input[type='text'], .window .wrap .line .edit input[type='email'], .window .wrap .line .edit input[type='password']:focus{ border: 1px solid gray; } .window .wrap .line .edit .error{ border-color: red; } .window .wrap .buttons{ position: absolute; width: 100%; height: 40px; left: 0; bottom: 0; background-color: rgb(240,240,240); color: rgb(40,40,40); } .window .wrap .buttons .button{ float: right; padding: 5px; border: 1px solid gray; margin: 5px; cursor: pointer; } .window .wrap .buttons .button:hover{ background-color: rgb(188, 188, 188); border: 1px solid gray; } 


Index.ejs рд╣реЛрдо
  <!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" > <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1" > <!-- CSS !--> <link rel="stylesheet" href="css/reset.css"> <link rel="stylesheet" href="css/style.css"> <!-- Utils !--> <script src="http://yandex.st/jquery/1.8.2/jquery.min.js"></script> <script src="js/jquery.cookie.js"></script> <script src="js/core.js"></script> <title>BigBrother - <%= title %></title> </head> <body> <div id="topmenu"> <div id="user"> Login </div> </div> <div class="window" id="login"> <div class="wrap"> <div class="header"> <div id="pagelogin" class="active">Login</div> / <div id="pageregister" class="">Registration</div> </div> <div class="line" style="margin-top: 80px"> <div class="label">Email:</div> <div class="edit"><input type="email" id="email"/></div> </div> <div class="line" style="margin-top: 10px"> <div class="label">Password:</div> <div class="edit"><input type="password" id="password"/></div> </div> <div class="line" style="margin-top: 10px" id="confirmationpassworddiv"> <div class="label">Confirmation password:</div> <div class="edit"><input type="password" id="confirmationpassword"/></div> </div> <div class="line" style="margin-top: 10px"> <div class="label">Stay online:</div> <div class="edit"><input type="checkbox" id="stayonline"/></div> </div> <div class="buttons"> <div class="button" id="logincancel">cancel</div> <div class="button" id="loginsbmt">login</div> </div> <div class="msg"> </div> </div> <div class="back"></div> </div> <script type="text/javascript"> jQuery(window).load(function(){ Init(); }); </script> </body> </html> 


Core.js рдИрд╡реЗрдВрдЯреНрд╕ рдФрд░ рд╣реИрдВрдбрд▓рд░
 function Init(){ /* LOGIN WINDOW */ //Init vars var user = jQuery('#user'); var loginwindow = jQuery('#login'); var loginemail = jQuery('#email'); var loginpassword = jQuery('#password'); var confirmationpassword = jQuery('#confirmationpassword'); var pagelogin = jQuery('#pagelogin'); var pageregister = jQuery('#pageregister'); var confirmationpassworddiv = jQuery('#confirmationpassworddiv'); var loginmsg = jQuery(jQuery('.msg',loginwindow)[0]); var stayonline = jQuery('#stayonline'); confirmationpassworddiv.hide(); loginmsg.hide(); //Set events confirmationpassword.keypress(inputkeypress); loginpassword.keypress(inputkeypress); loginemail.keypress(inputkeypress); user.click(function(){ loginwindow.fadeIn('fast'); }); jQuery('#logincancel').click(function(){ hidewindow(); }); jQuery('#loginsbmt').click(function(){ var isLogin = pagelogin.hasClass('active'); var error = false; var errormsg = ''; if (loginemail.val()===''){ error = true; loginemail.addClass('error'); errormsg += 'Type your email'; }else loginemail.removeClass('error'); if (!isLogin){ if (loginpassword.val()===''){ error = true; loginpassword.addClass('error'); errormsg += '<br/>Type your password'; }else loginpassword.removeClass('error'); if (confirmationpassword.val()===''){ error = true; confirmationpassword.addClass('error'); errormsg += '<br/>Type your confirmation password'; }else confirmationpassword.removeClass('error'); if (confirmationpassword.val()!=loginpassword.val()){ error = true; confirmationpassword.addClass('error'); errormsg += '<br/>Password not same'; } } if (!error) { if (!isLogin) registeruser(); else loginuser(); }else{ loginmsg.html(errormsg); loginmsg.show(); } }); jQuery('.back',loginwindow).click(function(){ hidewindow(); }); pagelogin.click(function(){ pagelogin.addClass('active'); pageregister.removeClass('active'); confirmationpassworddiv.hide(); loginmsg.hide(); jQuery('#loginsbmt').html('login'); }); pageregister.click(function(){ pagelogin.removeClass('active'); pageregister.addClass('active'); confirmationpassworddiv.show(); loginmsg.hide(); jQuery('#loginsbmt').html('register'); }); //Check login state checklogin(); //Other function function inputkeypress(){ if (jQuery(this).val()!=='') jQuery(this).removeClass('error'); } //Hide login window and clear state function hidewindow(){ loginwindow.fadeOut('fast',function(){ loginmsg.hide(); confirmationpassword.removeClass('error'); loginpassword.removeClass('error'); loginemail.removeClass('error'); pagelogin.click(); }); } //Do login function loginuser(){ jQuery.ajax({ type: "POST", url: "/auth", data: { email: loginemail.val(), password: loginpassword.val(), stayonline: stayonline.val()==='1'} }).done(function( msg ) { loginpassword.val(''); if (msg.error){ loginmsg.html(msg.msg); loginmsg.show(); }else{ jQuery.cookie('sessionid',msg.sessionid); loginmsg.html(msg.msg); loginmsg.show(); setTimeout(function() { hidewindow(); checklogin(); }, 1000); } }); } //Check login state function checklogin(){ user.html('loading...'); jQuery.ajax({ type: "GET", url: "/auth" }).done(function( msg ) { if (msg.error) { user.html('login'); user.unbind('click'); user.click(function(){loginwindow.fadeIn('fast');}); } else { user.html(msg.displayname); user.unbind('click'); user.click(logout); } }); } //Do log out function logout(){ jQuery.ajax({ type: "DELETE", url: "/auth" }).done(function(msg){ if (msg.error){ alert(msg.msg); return; } user.html('login'); user.unbind('click'); user.click(function(){ loginwindow.fadeIn('fast'); }); }); } //Register new user function registeruser(){ jQuery.ajax({ type: "POST", url: "/auth/register", data: { email: loginemail.val(), password: loginpassword.val(), confirmationpassword: confirmationpassword.val(), stayonline: stayonline.val()==='1'} }).done(function( msg ) { loginpassword.val(''); confirmationpassword.val(''); if (msg.error){ loginmsg.html(msg.msg); loginmsg.show(); }else{ loginmsg.html(msg.msg); loginmsg.show(); setTimeout(function() { hidewindow(); checklogin(); }, 1000); } }); } /* LOGIN WINDOW */ }; 



2. рд╕рд░реНрд╡рд░ рднрд╛рдЧ

рджрд┐рди рдХреА рд╢реБрд░реБрдЖрдд, рдПрдХ рдбреЗрдЯрд╛рдмреЗрд╕ рдирд┐рдпрдВрддреНрд░рдХ рдмрдирд╛рдПрдБред рдЗрд╕рдХрд╛ рдХрд╛рд░реНрдп рдбреЗрдЯрд╛рдмреЗрд╕ рд╕реЗ рдХрдиреЗрдХреНрдЯ рдХрд░рдирд╛ рд╣реИ, рд╕рд╛рде рд╣реА рдХреБрдЫ рдЕрдиреНрдп рдЕрдХреНрд╕рд░ рдЙрдкрдпреЛрдЧ рдХреА рдЬрд╛рдиреЗ рд╡рд╛рд▓реА рдкреНрд░рдХреНрд░рд┐рдпрд╛рдУрдВ рдХреЛ рд╡рд╣рд╛рдВ рд╕рдВрдЧреНрд░рд╣реАрдд рдХрд┐рдпрд╛ рдЬрд╛рдПрдЧрд╛, /controllers/db.js :
 exports.opendb = function(settings, callback){ var mongo = require('mongodb'), Server = mongo.Server, Db = mongo.Db; var server = new Server(settings.host, settings.port, {auto_reconnect: settings.auto_reconnect}); var db = new Db(settings.db, server); db.open(function(err, db) { if(!err) { db.authenticate(settings.username, settings.password, function(){callback(false, db);}); } else callback(true, db); }); }; exports.criptpassword = function(string){ var crypto = require('crypto'); return crypto.createHash('md5').update(string+global.saldo).digest("hex"); }; 

рдХрд╛рдо рдХрд░рддреЗ рд╕рдордп рдЪрд░ рдХрд╛ рдПрдХ рдмрдбрд╝рд╛ рдЧреБрдЪреНрдЫрд╛ рдЙрддреНрдкрдиреНрди рдирд╣реАрдВ рдХрд░рдиреЗ рдХреЗ рд▓рд┐рдП, рдирд┐рдпрдВрддреНрд░рдХ рдлрд╝реЛрд▓реНрдбрд░ рдореЗрдВ рдПрдХ index.js рдлрд╝рд╛рдЗрд▓ рдмрдирд╛рдПрдВред рдЗрд╕ рдорд╛рдорд▓реЗ рдореЗрдВ, рдЕрдЧрд░ рд╣рдо рд▓рд┐рдЦрддреЗ рд╣реИрдВ
 global.controllers = require('./controllers'); 

рд╣рдорд╛рд░реЗ index.js рдХреЛ рд╡реИрд░рд┐рдПрдмрд▓ рдореЗрдВ рдЬреЛрдбрд╝рд╛ рдЬрд╛рдПрдЧрд╛:
 exports.db = require('./db'); exports.users = require('./users'); exports.stayonlinesessions = require('./stayonlinesessions'); 

рдЕрдЧрд▓рд╛ рдЪрд░рдг рд╕реНрдЯрд╛рд░реНрдЯ рдкреЗрдЬ /routers/index.js рдХреЗ рд▓рд┐рдП рдПрдХ рд░рд╛рдЙрдЯрд░ рдмрдирд╛рдирд╛ рд╣реИ
 exports.index = function(req, res){ res.render('index',{title: 'Home'}); }; exports.auth = require('./auth'); 

рд╣рдо рдЕрдкрдиреЗ рдбреЗрдЯрд╛рдмреЗрд╕ рдореЗрдВ рдЙрдкрдпреЛрдЧрдХрд░реНрддрд╛ рд╕рддреНрд░реЛрдВ рдХреЛ рд╕рдВрдЧреНрд░рд╣реАрдд рдХрд░реЗрдВрдЧреЗ, рд╣рдо рдЗрд╕рдХреЗ рд▓рд┐рдП рдХрдиреЗрдХреНрдЯ-рдореЛрдВрдЧреЛ рдкреИрдХреЗрдЬ рдХрд╛ рдЙрдкрдпреЛрдЧ рдХрд░рддреЗ рд╣реИрдВ ред
рдФрд░ рд╡рд╛рд╕реНрддрд╡ рдореЗрдВ рд╣рдорд╛рд░реЗ server.js ред
рд╡реИрд╢реНрд╡рд┐рдХ рдореЙрдбреНрдпреВрд▓ рдФрд░ рд╡реИрд░рд┐рдПрдмрд▓ рдХреЛ рдкреНрд░рд╛рд░рдВрдн рдХрд░реЗрдВ
 //Modules var express = require("express"); var app = express(); var MongoStore = require('connect-mongo')(express); var dbsettings = require('./config').settings; //Server configuration global.saldo = 'fewfwef352tFRWEQF'; global.controllers = require('./controllers'); //Controllers var routers = require('./routers'); //Routers var viewEngine = 'ejs'; 

рдПрдХреНрд╕рдкреНрд░реЗрд╕ рд╕рд░реНрд╡рд░ рдХреЙрдиреНрдлрд╝рд┐рдЧрд░ рдХрд░реЗрдВ
 // Configuration app.configure(function(){ app.set('views', __dirname + '/views'); app.set('view engine', viewEngine); app.use(express.cookieParser()); app.use(express.session({ secret: 'fegwegwe', store: new MongoStore(dbsettings), cookie: { path: '/', httpOnly: true, maxAge: 1000*60*60*24 } })); app.use(express.bodyParser()); app.use(express.methodOverride()); app.use(app.router); app.use(express.static(__dirname + '/public')); }); 

рдПрдХ рдЫреЛрдЯреА рд╕реА рд╡рд┐рд╖рдпрд╛рдВрддрд░рддрд╛ рдФрд░ рдЬрдЯрд┐рд▓рддрд╛ рд╣реИ рдЬрд┐рд╕рдХрд╛ рдореИрдВрдиреЗ рд╕рд╛рдордирд╛ рдХрд┐рдпрд╛ред рдпрджрд┐ рдЖрдк рдорд╛рд░реНрдЧреЛрдВ рдХреЛ рдирд┐рд░реНрдзрд╛рд░рд┐рдд рдХрд░рдиреЗ рдХреЗ рдмрд╛рдж рд╕рддреНрд░реЛрдВ рдХреЗ рдЖрд░рдВрднреАрдХрд░рдг рдХреЛ рдирд┐рд░реНрджрд┐рд╖реНрдЯ рдХрд░рддреЗ рд╣реИрдВ, рддреЛ рд╕рддреНрд░ рдХрд┐рд╕реА рдХрд╛рд░рдг рд╕реЗ рдХрд╛рдо рдирд╣реАрдВ рдХрд░рддреЗ рд╣реИрдВ, рдЗрд╕рдХреЗ рд▓рд┐рдП рд╣рдо рдкрд╣рд▓реЗ рд╕рддреНрд░ рдХреЛ рдкреНрд░рд╛рд░рдВрдн рдХрд░рддреЗ рд╣реИрдВред
рд╕рд╣реА рдврдВрдЧ рд╕реЗ

 // Configuration app.configure(function(){ app.set('views', __dirname + '/views'); app.set('view engine', viewEngine); /*---------------------------------------------------*/ app.use(express.cookieParser()); app.use(express.session({ secret: 'fegwegwe', store: new MongoStore(dbsettings), cookie: { path: '/', httpOnly: true, maxAge: 1000*60*60*24 } })); /*---------------------------------------------------*/ app.use(express.bodyParser()); app.use(express.methodOverride()); app.use(app.router); app.use(express.static(__dirname + '/public')); }); 


рдЧрд╝рд▓рдд

 // Configuration app.configure(function(){ app.set('views', __dirname + '/views'); app.set('view engine', viewEngine); app.use(express.bodyParser()); app.use(express.methodOverride()); app.use(app.router); app.use(express.static(__dirname + '/public')); /*---------------------------------------------------*/ app.use(express.cookieParser()); app.use(express.session({ secret: 'fegwegwe', store: new MongoStore(dbsettings), cookie: { path: '/', httpOnly: true, maxAge: 1000*60*60*24 } })); /*---------------------------------------------------*/ }); 

рд╣рдордиреЗ рдорд╛рд░реНрдЧ рдирд┐рд░реНрдзрд╛рд░рд┐рдд рдХрд┐рдП
 app.get('/',routers.index); //  app.post('/auth/register',routers.auth.register); //  app.post('/auth',routers.auth.login); //   app.get('/auth',routers.auth.getlogin); //  :    app.del('/auth',routers.auth.logout); //   

рдбреЗрдЯрд╛рдмреЗрд╕ рд╕реЗ рдХрдиреЗрдХреНрдЯ рдХрд░реЗрдВ, рдпрджрд┐ рд╕рдлрд▓ рд╣реЛ, рддреЛ рд╕рд░реНрд╡рд░ рд╢реБрд░реВ рдХрд░реЗрдВ
 //Connect to db and start global.controllers.db.opendb(dbsettings, function(error,db){ if (!error){ global.db = db; app.listen(process.env.PORT); } else console.log('Error connect to db'); }); 

рд╣рдо рд╢реБрд░реВ рдХрд░рдиреЗ рдХреА рдХреЛрд╢рд┐рд╢ рдХрд░рддреЗ рд╣реИрдВ, рдореБрдЦреНрдп рдкреГрд╖реНрда рдХреЛ рджреЗрдЦрддреЗ рд╣реИрдВ, рдпрд╣ рдХрд╛рдо рдХрд░рддрд╛ рд╣реИред
рдЕрдм рд╣рдо рд╕рд╛рдЗрдЯ рдЙрдкрдпреЛрдЧрдХрд░реНрддрд╛рдУрдВ рдХреЗ рд▓рд┐рдП рдЬрд┐рдореНрдореЗрджрд╛рд░ рдирд┐рдпрдВрддреНрд░рдХ рдХрд╛ рд╡рд░реНрдгрди рдХрд░реЗрдВрдЧреЗ: /controllers/users.js
рдЙрдкрдпреЛрдЧрдХрд░реНрддрд╛ рдкрдВрдЬреАрдХрд░рдг:
 exports.register = function(email, password, callback){ global.db.collection('users', function(err, collection) { if (err){ callback(true); return; } collection.findOne({email: email.toLowerCase()}, function(eror, item){ if (item === null){ collection.insert({email: email.toLowerCase(), password: global.controllers.db.criptpassword(password), emailchack: true, roles: []},{safe:true},function(error, result){ if (err){ callback(true); return; } callback(false, result[0]); }); }else callback(true, null, 'User already exists'); }); }); }; 

рдЖрдИрдбреА рджреНрд╡рд╛рд░рд╛ рдЙрдкрдпреЛрдЧрдХрд░реНрддрд╛ рдкреНрд░рд╛рдкреНрдд рдХрд░реЗрдВ
 exports.getuser = function(id, callback){ global.db.collection('users', function(err, collection) { if (err){ callback(true); return; } var ObjectID = require('mongodb').ObjectID; if (typeof id === 'string') id = new ObjectID(id); collection.findOne({_id: id}, function(eror, item){ if (err){ callback(true); return; } callback(false, item); }); }); }; 

рдЬрд╛рдВрдЪреЗрдВ рдХрд┐ рдХреНрдпрд╛ рдЙрдкрдпреЛрдЧрдХрд░реНрддрд╛ рдХреЗ рд▓рд┐рдП рд▓реЙрдЧ рдЗрди рдХрд░рдирд╛ рд╕рдВрднрд╡ рд╣реИ
 exports.checkuser = function(email, password, callback){ global.db.collection('users', function(err, collection) { if (err){ callback(true); return; } collection.findOne({email: email.toLowerCase(), password: global.controllers.db.criptpassword(password)}, function(error,item){ if (item === null) item = {}; callback(error, item.emailchack, item); }); }); }; 

рдпрджрд┐ рдЙрдкрдпреЛрдЧрдХрд░реНрддрд╛ "рдореБрдЭреЗ рдпрд╛рдж рд░рдЦреЗрдВ" рдЪреЗрдХрдмреЙрдХреНрд╕ рдкрд░ рдЯрд┐рдХ рдХрд░рддрд╛ рд╣реИ, рддреЛ рд╣рдо рдХреБрдХреАрдЬрд╝ рдореЗрдВ рд╣реИрд╢реЗрдб рдЙрдкрдпреЛрдЧрдХрд░реНрддрд╛ рдЖрдИрдбреА рдЬреЛрдбрд╝ рджреЗрдВрдЧреЗ рдФрд░ рд╣рдо "рд░рд┐рдУрдирд▓рд╛рдЗрдирд╢реАрдЯреНрд╕" рджрд╕реНрддрд╛рд╡реЗрдЬрд╝ рдирд┐рдпрдВрддреНрд░рдХ рдореЗрдВ рд╕рдм рдХреБрдЫ рд╕рдВрдЧреНрд░рд╣реАрдд рдХрд░реЗрдВрдЧреЗред рдЪрд▓рд┐рдП рд╕реЗрд╢рди / рдХрдВрдЯреНрд░реЛрд▓рд░ рдХреЗ рд▓рд┐рдП рдЬрд┐рдореНрдореЗрджрд╛рд░ рдХрдВрдЯреНрд░реЛрд▓рд░ рд▓рд┐рдЦрддреЗ рд╣реИрдВ
  exports.savesession = function(user_id, callback){ global.db.collection('stayonlinesessions', function(err, collection) { if (err){ callback(true); return; } var hash = global.controllers.db.criptpassword(user_id.toString()); collection.insert({user_id: user_id, hash: hash, createdate: new Date()},{safe:true},function(error, result){ if (err){ callback(true); return; } callback(false, hash); }); }); }; exports.getsession = function(hash, callback){ global.db.collection('stayonlinesessions', function(err, collection) { if (err){ callback(true); return; } collection.findOne({hash: hash}, function(error,item){ if (err || item === null){ callback(true); return; } global.controllers.users.getuser(item.user_id, function(error, user){ if (err || user === null){ callback(true); return; } callback(false, user); }); }); }); }; exports.delsession = function(hash, callback){ global.db.collection('stayonlinesessions', function(err, collection) { if (err){ callback(true); return; } collection.remove({hash: hash}, {safe: true}, function(err,removed){ if (err || !removed){ callback(true); return; } callback(false); }); }); }; 

рдЗрд╕рд▓рд┐рдП, рд╣рдорд╛рд░реЗ рдкрд╛рд╕ рдРрд╕реЗ рдирд┐рдпрдВрддреНрд░рдХ рд╣реИрдВ рдЬреЛ рдкрдВрдЬреАрдХрд░рдг рдФрд░ рд╕рддреНрд░реЛрдВ рдХреЗ рд▓рд┐рдП рдЬрд┐рдореНрдореЗрджрд╛рд░ рд╣реИрдВ, рдЕрдм рд╣рдореЗрдВ рдкреНрд░рд╛рдзрд┐рдХрд░рдг рдорд╛рд░реНрдЧреЛрдВ рдХреЛ рд▓рд╛рдЧреВ рдХрд░рдиреЗ рдХреА рдЖрд╡рд╢реНрдпрдХрддрд╛ рд╣реИред рд╣рдо рдЗрди рдХрд╛рд░реНрдпреЛрдВ /routers/auth.js рдХреЗ рд▓рд┐рдП рдЬрд┐рдореНрдореЗрджрд╛рд░ рд╣реЛрдВрдЧреЗ ред
рдЙрдкрдпреЛрдЧрдХрд░реНрддрд╛ рдкрдВрдЬреАрдХрд░рдг
 exports.register = function(req, res){ var email = req.body.email; var password = req.body.password; var confirmationpassword = req.body.confirmationpassword; var stayonline = req.body.stayonline; //   var check = require('validator').check; if (!check(email).len(6, 64).isEmail() || !check(password).notNull() || !check(password).equals(confirmationpassword)){ res.send({ error: true, msg: 'Check your' }); return; } //   global.controllers.users.register(email, password, function(error, user, msg){ if (error){ if (typeof msg == 'undefined' || msg===null ) msg = 'Register error'; res.send({ error: true, msg: msg }); return; } //  req.session.authorized = true; req.session.user_id = user._id; req.session.username = user.email; //   " "        if (stayonline){ global.controllers.stayonlinesessions.savesession(user._id, function(error, hash){ res.send({ error: false, msg: 'Success register email: '+email, sessionid: hash }); }); }else{ res.send({ error: false, msg: 'Success register email: '+email }); } }); }; 

рд╕рд╛рдЗрдЯ рдкрд░ рд▓реЙрдЧрд┐рди рдХрд░реЗрдВ
 exports.login = function(req, res){ var email = req.body.email; var password = req.body.password; var stayonline = req.body.stayonline; global.controllers.users.checkuser(email, password, function(error, canlogin, user){ if (error || !canlogin){ res.send({ error: true, msg: 'Check your email or password' }); return; } req.session.authorized = true; req.session.user_id = user._id; req.session.username = user.email; if (stayonline){ global.controllers.stayonlinesessions.savesession(user._id, function(error, hash){ res.send({ error: false, msg: 'Success login email: '+user.email, sessionid: hash }); }); } else { res.send({ error: false, msg: 'Success login email: '+user.email }); } }); }; 

рдмрд╛рд╣рд░ рдирд┐рдХрд▓реЗрдВ рд╕рд╛рдЗрдЯ
 exports.logout = function(req, res){ if (!req.session.authorized){ res.send({ error: true, msg: 'You are not loggined' }); return; } req.session.authorized = false; delete req.session.username; delete req.session.user_id; //     ,    if (typeof req.cookies.sessionid !== 'undefined' && req.cookies.sessionid !== ''){ global.controllers.stayonlinesessions.delsession(req.cookies.sessionid, function(error){ if (error){ console.log('Session was not deleted'); return; } }); } res.send({ error: false }); }; 

рд╡рд░реНрддрдорд╛рди рдкреНрд░рд╛рдзрд┐рдХрд░рдг рд╕реНрдерд┐рддрд┐ рдкреНрд░рд╛рдкреНрдд рдХрд░рдирд╛
 exports.getlogin = function(req, res){ if (!req.session.authorized){ //   ,         global.controllers.stayonlinesessions.getsession(req.cookies.sessionid, function(error, user){ if (error){ res.send({ error: true, msg: 'You are not loggined' }); return; } req.session.authorized = true; req.session.user_id = user._id; req.session.username = user.email; res.send({ error: false, displayname: req.session.username }); }); }else{ res.send({ error: false, displayname: req.session.username }); } }; 

рдХреБрдХреАрдЬрд╝ рдХреЗ рдмрд╛рд░реЗ рдореЗрдВ рдХреБрдЫ рдмрд┐рдВрджреБред рдпрджрд┐ рд╕рддреНрд░ рдореЙрдбреНрдпреВрд▓ рдХреЗ рдкреНрд░рд╛рд░рдВрдн рдХреЗ рджреМрд░рд╛рди рд╣рдо рднрдВрдбрд╛рд░рдг рд╕рдордп рдирд┐рд░реНрджрд┐рд╖реНрдЯ рдирд╣реАрдВ рдХрд░рддреЗ рд╣реИрдВ, рддреЛ рд╡реЗ рдмреНрд░рд╛рдЙрдЬрд╝рд░ рдореЗрдВ рдмрд┐рд▓реНрдХреБрд▓ рднреА рд╕рдВрдЧреНрд░рд╣реАрдд рдирд╣реАрдВ рд╣реИрдВред рдпрд╣ рдПрдХ рд╕рд╛рдзрд╛рд░рдг рд╕реЗрдЯрдЕрдк рдХреЗ рд╕рд╛рде рд╡реНрдпрд╡рд╣рд╛рд░ рдХрд┐рдпрд╛ рдЬрд╛рддрд╛ рд╣реИред
 app.use(express.session({ secret: 'fegwegwe', store: new MongoStore(dbsettings), cookie: { path: '/', httpOnly: true, maxAge: 1000*60*60*24 } // maxAge -     })); 

рдХреНрд▓рд╛рдЗрдВрдЯ рдХреА рддрд░рдл, рдХреБрдХреАрдЬрд╝ рдХреЗ рд╕рд╛рде рдХрд╛рдо рдХрд░рдирд╛ рдХрд╛рдлреА рд╕рд░рд▓ рд╣реИред рдореИрдВрдиреЗ jquery.cookie.js рдкреНрд▓рдЧрдЗрди рдХрд╛ рдЙрдкрдпреЛрдЧ рдХрд┐рдпрд╛
 jQuery.cookie('sessionid',msg.sessionid); 

рдирддреАрдЬрддрди, рд╣рдореЗрдВ рдкреНрд░рд╛рдзрд┐рдХрд░рдг, рдкрдВрдЬреАрдХрд░рдг, "рдореБрдЭреЗ рдпрд╛рдж рд░рдЦреЗрдВ" рдлрд╝рдВрдХреНрд╢рди рдкреНрд░рд╛рдкреНрдд рд╣реБрдЖ, рдлрд┐рд░ рд╣рдореЗрдВ рдИрдореЗрд▓ рдкрддреЗ рдХреА рдкреБрд╖реНрдЯрд┐, рдкрд╛рд╕рд╡рд░реНрдб рдкреБрдирд░реНрдкреНрд░рд╛рдкреНрддрд┐ рдХреЗ рд╕рд╛рде рдПрдХ рдИрдореЗрд▓ рднреЗрдЬрдирд╛ рд╣реЛрдЧрд╛, рд▓реЗрдХрд┐рди рдпрд╣ рдЕрдЧрд▓реА рдмрд╛рд░ рд╣реИред

Source: https://habr.com/ru/post/In154755/


All Articles