Skip to content

Commit 3d877bc

Browse files
committed
mongoDb学习
1 parent c30c7be commit 3d877bc

File tree

6 files changed

+195
-0
lines changed

6 files changed

+195
-0
lines changed

nodejs/相关框架/mongoDB/app.js

Lines changed: 130 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,130 @@
1+
/**
2+
* 模块依赖
3+
*/
4+
5+
var express = require("express"),
6+
mongodb = require('mongodb'),
7+
ObjectId = mongodb.ObjectID;
8+
9+
/**
10+
* 构建应用程序
11+
*/
12+
var app = express.createServer();
13+
14+
15+
/**
16+
* 中间件
17+
*/
18+
app.use(express.bodyParser());
19+
app.use(express.cookieParser());
20+
app.use(express.session({secret: 'my secret'}));
21+
22+
//身份验证中间件
23+
app.use(function (req, res, next) {
24+
if (req.session.loggedIn) {
25+
res.local('authenticated', true);
26+
//app.users.findOne({_id: {$oid: req.session.loggedIn}}, function (err, doc) {
27+
app.users.findOne({"_id": ObjectId(req.session.loggedIn)}, function(err, doc) {
28+
if (err) return next(err);
29+
res.local('me', doc);
30+
next();
31+
})
32+
} else {
33+
res.local('authenticated', false);
34+
next();
35+
}
36+
});
37+
38+
//模板路径
39+
app.set('views', './views/pages');
40+
41+
/**
42+
* 连接数据库
43+
*/
44+
var server = new mongodb.Server('127.0.0.1', 27017);
45+
new mongodb.Db('my-website', server).open(function (err, client) {
46+
47+
//如果失败,终止进程
48+
if (err) throw err;
49+
50+
//连接成功,打印成功信息
51+
console.log('\033[96m + \033[39m connected to mongodb');
52+
53+
//建立连接
54+
app.users = new mongodb.Collection(client, 'users');
55+
56+
//建立索引
57+
client.ensureIndex('users', 'email', function (err) {
58+
if (err) throw err;
59+
60+
client.ensureIndex('users', 'password', function (err) {
61+
if (err) throw err;
62+
63+
console.log('\033[96m + \033[39m ensured indexes');
64+
65+
//监听端口
66+
app.listen(3000, function () {
67+
console.log('\033[96m + \033[39m app listening on *:3000');
68+
});
69+
})
70+
});
71+
});
72+
73+
/**
74+
* 指定视图选项
75+
*/
76+
app.set('view engine', 'jade');
77+
78+
//若使用了Express 3,则不需要下面这行代码
79+
app.set('view options', {layout: false});
80+
81+
82+
/**
83+
* 路由
84+
*/
85+
86+
//默认路由
87+
app.get('/', function (req, res) {
88+
res.render('index');
89+
});
90+
91+
//登录路由
92+
app.get('/login', function (req, res) {
93+
res.render('login', {signupEmail: ''});
94+
});
95+
96+
app.get('/login/:signupEmail', function (req, res) {
97+
res.render('login', {signupEmail: req.params.signupEmail});
98+
});
99+
100+
app.post('/login', function (req, res) {
101+
app.users.findOne({email: req.body.user.email, password: req.body.user.password}, function (err, doc) {
102+
if (err) return next(err);
103+
if (!doc) return res.send('<p>用户名或密码错误</p>');
104+
req.session.loggedIn = doc._id.toString();
105+
res.redirect('/');
106+
});
107+
});
108+
109+
110+
//注册路由
111+
app.get('/signup', function (req, res) {
112+
res.render('signup');
113+
});
114+
115+
app.post('/signup', function (req, res, next) {
116+
app.users.insert(req.body.user, function (err, doc) {
117+
118+
//如果遇到错误,调用next,这样就快要显示一个"错误500"页面
119+
if (err) return next(err);
120+
121+
//插入数据成功后,重定向到登录页,并传email的值过去
122+
res.redirect('/login/' + doc[0].email);
123+
});
124+
});
125+
126+
//退出登录
127+
app.get('/logout',function(req,res){
128+
req.session.loggedIn=null;
129+
res.redirect('/');
130+
});
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
{
2+
"name": "user-auth-example",
3+
"version": "0.0.1",
4+
"dependencies": {
5+
"express": "2.5.8",
6+
"mongodb": "0.9.9",
7+
"jade": "0.20.3"
8+
}
9+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
doctype
2+
html
3+
head
4+
title MongoDb example
5+
body
6+
h1 My first MongoDB app
7+
hr
8+
block body
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
extends ../layout
2+
block body
3+
if(authenticated)
4+
p welcom back,#{me.first}
5+
a(href="/logout") Logout
6+
else
7+
p welcom new visitor!
8+
ul
9+
li: a(href="/login") Login
10+
li: a(href="/signup") Signup
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
extends ../layout
2+
block body
3+
if(signupEmail)
4+
p Congratulations on signing up! Please login below.
5+
form(action="/login",method="POST")
6+
fieldset
7+
legend Log up
8+
p
9+
label Email
10+
input(name="user[email]",type="text",value=signupEmail)
11+
p
12+
label Password
13+
input(name="user[password]",type="text")
14+
p
15+
button Submit
16+
p
17+
a(href="/") Go back
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
extends ../layout
2+
block body
3+
form(action="/signup",method="POST")
4+
fieldset
5+
legend Sign up
6+
p
7+
label First
8+
input(name="user[first]",type="text")
9+
p
10+
label Last
11+
input(name="user[last]",type="text")
12+
p
13+
label Email
14+
input(name="user[email]",type="text")
15+
p
16+
label Password
17+
input(name="user[password]",type="text")
18+
p
19+
button Submit
20+
p
21+
a(href="/") Go back

0 commit comments

Comments
 (0)