Details of node. js + MongoDB Project (ii)

Source: Internet
Author: User
Tags findone

The first step

Of course, the preparation of work, in the bootstrap official website Download Good things, how to use the official website has been written in detail, in this is not detailed.
: http://v3.bootcss.com/getting-started/

Step Two
is to log in and register.

Written in the router file
Index.js

/*ucenter-Login */router.post ('/ucenter/login ', function  (req, res)  {     user.findone ({name: req.body.name}, function  (err, data)  {         if  (data.name === req.body.name &&  Data.password ===  req.body.password)  {              console.log (req.body.name +  ' Landing Success '  + new date ());              res.render (' Ucenter ',  {title:   ' Ucenter '});        } else {              console.log (ERR);              res.send (+);         }    }); 

/*ucenter-registered */router.post ('/ucenter/register ', function  (req, res)  {     user.findone ({name: req.body.name}, function  (Err, docs)  {         if  (ERR)              console.log (Err);        else if  (!docs)  {             user.create ({                 name: req.body.name,                 password: req.body.password             }, function  (Err, doc)  {                if   (ERR)                      Console.log (ERR);                 else                     console.log (DOC);             });             res.render (' Ucenter ',  { title:  ' Ucenter '});         }    });

The form submitted on the page with the action, not with Ajax, here for the convenience of explanation, if you write with the Ajax request writing
See: The Express Course in Http://www.hubwiz.com/coursecenter
There is a detailed approach.

Step Three

That's the problem with the express session.

Prior to express 4.0, middleware like session was automatically installed with Express,
Some tutorials on the web use the Express version just before 4.0, so be sure to note this when using the 4.0 and later versions.

This is required to implement the MongoDB callback to build Connect-mongo.

 with express4:var session    = require (' express-session ');var  Mongostore = require (' Connect-mongo ') (session), App.use (session ({    secret:  settings.cookie_secret,    store: new mongostore ({       db : settings.db,    })   }); With express<4:var express = require (' Express '); Var mongostore = require (' Connect-mongo ') (express); App.use (Express.session ({    secret: settings.cookie_secret,     store: new mongostore ({      db: settings.db     })   }); With connect:var connect = require (' Connect '); Var mongostore = require (' Connect-mongo ') (connect); 

This code can then be seen in Connect-mongo GitHub.

The code hasn't been collated yet, and it's not uploaded to GitHub, so keep an eye on my blog.
All right, it's over.


node. js + MongoDB To do the Project (ii)

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.