Nodejs Learning (2) middleware

Source: Internet
Author: User

varConnect=require (' Connect '), Morgan=require (' Morgan '),//LogBodyparser=require (' Body-parser '), Session=require (' Cookie-sessions '), the users=require ('./users '),//test DataServer=Connect (); Server.use (Morgan (": Method:url:res[content-type]: Response-time"));//Record Request method, URL, response header, and response timeServer.use (bodyparser.urlencoded ({extended:false }));    Server.use (Bodyparser.json ()); Server.use (Session ({secret:' My app secret '})); Server.use (function(Req,res,next) {//If you are logged on        if('/' ==req.url &&session.logged_in) {Res.writehead (200,{"Content-type": "Text/html"}); Res.end ("Welcome back,<b>" +session["username"]+ ' </b>. ' + ' <a href= '/logout ' >logout</a> '); }Else{next ();    }    }); Server.use (function(Req,res,next) {//Show form when not logged on        if("/" ==req.url && req.method== "GET") {Res.writehead (200,{"Content-type": "Text/html"}); Res.end ([' <form action= '/login "method=" POST ">",                ' <fieldset> ',                ' <legend>please log in</legend> ',                ' <p>user:<input type= ' text ' name= ' User ' ></p> ',                ' <p>password:<input type= ' Password "name=" Password "></p>",                ' <button>Submit</button> ',                ' </fieldset> ',                ' </form> '].join (‘‘)); }Else{next ();    }    }); Server.use (function(Req,res,next) {//Login Successful        if("/login" ==req.url && "POST" = =Req.method) {Res.writehead (200); if(!users[req.body.user] | | users[req.body.user].password!==Req.body.password) {Res.end (' Bad Username/password '); }Else{session.logged_in=true; Session.username=users[req.body.user]["Name"]; Res.end ("Authenticated"); }        }Else{next ();    }    }); Server.use (function(Req,res,next) {//Log Out        if("/logout" = =Req.url) {session.logged_in=false; Res.writehead (200); Res.end (' Logged out '); }Else{next (); }}) Server.listen (3000);

Users.json

{    "Tobi": {        "password": "Ferret",        "name": "Tobi Holowaychuk"    }} 

Nodejs Learning (2) middleware

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.