As a beginner, I also encountered a lot of problems when doing this example, in the view of some Daniel's solution, finally achieved this simple effect, I will share this source code, and the problems I encountered
This is an example of the NODEJS environment, using the Jade,express,mongodb of the main modules
Layout.jade file
DOCTYPE 5
Html
Head
Title Mongodb Example
Body
H1 my first MongoDB app
hr
Block content
Index.jade file
Extends. /layout
Block content
if (authenticated)
P Welcome back, #{me.first}
A (href= "/logout") logout
Else
P Welcome New Visitor
Ul
Li
A (href= "/login") login
Li
A (href= "/signup") signup
Login.jade file
Extends. /layout
Block content
Form (action= "/login", method= "POST")
FieldSet
Legend Log in
if (Signupemail)
P Congratulations on Signup!pleasel login below
P
Label email
Input (name= "User[email]", type= "text", Value=signupemail)
P
Label password
Input (name= "User[password]", type= "password")
P
Button Submit
P
A (href= "/") go back
Signup.jade file
Extends. /layout
Block content
Form (action= "/signup", method= "POST")
FieldSet
Legend Sign Up
P
Label first
Input (name= "User[first]", type= "text")
P
Label last
Input (name= "user[last]", type= "text")
P
Label email
Input (name= "User[email]", type= "text")
P
Label password
Input (name= "User[password]", type= "password")
P
Button Submit
P
A (href= "/") go back
Server.js file
var express = require (' Express ');
var app = Express.createserver ();
var ObjectId = require (' MongoDB '). ObjectID;
App.use (Express.bodyparser ());
App.use (Express.cookieparser ());
App.use (Express.session ({secret: ' My Secret '}));
App.use (function (req,res,next) {//This middleware must be put in front, put in the definition of the route after the no effect, this is the authentication middleware, pay particular attention to matching database _ ID to first convert the resulting ID string to Objectid object Objectid (req.session.loggedIn)
if (req.session.loggedIn)
{
Console.log (1);
Res.local (' authenticated ', true);
App.users.findOne ({_id:objectid (req.session.loggedIn)},function (Err,doc) {
if (ERR) return next (ERR);
Console.log (DOC);
Res.local (' Me ', doc);
Next ();
})
}else
{
Res.local (' authenticated ', false);
Res.local (' Signupemail ', false);
Next ();
}
})
App.set (' views ', './views/pages ');
App.set (' View engine ', ' Jade ');
App.set (' view options ', {layout:false});
App.get ('/', function (req,res) {//define home route
Res.render (' index '/*,{authenticated:false}*/);
})
App.get ('/login ', function (req,res) {//define login page Routing
Res.render (' login ');
})
App.get ('/login/:id ', function (req,res) {//login route, applied to just after registration
Res.render (' login ', {signupEmail:req.params.id});
})
App.post ('/login ', function (req,res) {//Handle login route
App.users.findOne ({email:req.body.user.email,password:req.body.user.password},function (Err,doc) {
if (ERR) return next (ERR);
if (!doc) return res.end (' User not find, go back and try Again ');
Console.log (DOC);
Req.session.loggedIn = Doc._id.tostring ();
Console.log (Req.session.loggedIn);
Res.redirect ('/');
})
})
App.get ('/logout ', function (req,res) {//Logout out
Req.session.loggedIn = null;
Res.redirect ('/');
})
App.get ('/signup ', function (req,res,next) {//Register route
Res.render (' signup ')
})
App.post ('/signup ', function (req,res,next) {//Handle registered route
App.users.insert (Req.body.user,function (Err,doc) {
Console.log (Req.body.user)
Console.log (DOC);
Contents of/*doc {result: {ok:1, N:1},
Ops
[{first: ' 66 ',
Last: ' 77 ',
Email: ' 88 ',
Password: ' 99 ',
_id:58d1248a72b183070d1cf678}],
Insertedcount:1,
Insertedids: [58d1248a72b183070d1cf678]}
*/
if (ERR) return next (ERR);
Res.redirect ('/login/' +doc.ops[0].email);
})
})
var MongoDB = require (' MongoDB '). Db;
var server = require (' MongoDB '). Server;
var db = new MongoDB (' My-website ', new server (' 127.0.0.1 ', 27017));//Connect to Database
Db.open (function (err,client) {
if (err) throw err;
Console.log (' connected to MongoDB ');
App.users = db.collection (' users ');//Create a collection named users
Client.ensureindex (' users ', ' email ', function (err) {
if (err) throw err;
Client.ensureindex (' Users ', ' Password ', function (err) {
if (err) throw err;
Console.log (' ensured index ');
App.listen (3000,function () {
Console.log (' app listening on 3000 ');
})
})
})
})
App.listen (3000);
I need to install MongoDB and Nodejs, this to the corresponding official website is easy to get down!
MongoDB makes a simple sign-in effect