Skip to content

Commit b93405e

Browse files
committed
mongoose学习
1 parent 3d877bc commit b93405e

File tree

10 files changed

+179
-0
lines changed

10 files changed

+179
-0
lines changed

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

Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
/**
2+
* 模块依赖
3+
*/
4+
5+
var express = require("express"),
6+
mongoose = require('mongoose'),
7+
User=require('./schemas/data');
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": ObjectId(req.session.loggedIn)}, function(err, doc) {
27+
User.findById(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+
45+
mongoose.connect('mongodb://127.0.0.1/my-website');
46+
47+
//监听端口
48+
app.listen(3000, function () {
49+
console.log('\033[96m + \033[39m app listening on *:3000');
50+
});
51+
52+
/**
53+
* 指定视图选项
54+
*/
55+
56+
app.set('view engine', 'jade');
57+
58+
//若使用了Express 3,则不需要下面这行代码
59+
app.set('view options', {layout: false});
60+
61+
62+
/**
63+
* 路由
64+
*/
65+
66+
//默认路由
67+
app.get('/', function (req, res) {
68+
res.render('index');
69+
});
70+
71+
//登录路由
72+
app.get('/login', function (req, res) {
73+
res.render('login', {signupEmail: ''});
74+
});
75+
76+
app.get('/login/:signupEmail', function (req, res) {
77+
res.render('login', {signupEmail: req.params.signupEmail});
78+
});
79+
80+
app.post('/login', function (req, res) {
81+
//app.users.findOne({email: req.body.user.email, password: req.body.user.password}, function (err, doc) {
82+
User.findOne({email: req.body.user.email, password: req.body.user.password}, function (err, doc) {
83+
if (err) return next(err);
84+
if (!doc) return res.send('<p>用户名或密码错误</p>');
85+
req.session.loggedIn = doc._id.toString();
86+
res.redirect('/');
87+
});
88+
});
89+
90+
91+
//注册路由
92+
app.get('/signup', function (req, res) {
93+
res.render('signup');
94+
});
95+
96+
app.post('/signup', function (req, res, next) {
97+
//app.users.insert(req.body.user, function (err, doc) {
98+
var user = new User(req.body.user).save(function (err,doc) {
99+
//如果遇到错误,调用next,这样就快要显示一个"错误500"页面
100+
if (err) return next(err);
101+
102+
//插入数据成功后,重定向到登录页,并传email的值过去
103+
res.redirect('/login/' + doc.email);
104+
});
105+
});
106+
107+
//退出登录
108+
app.get('/logout', function (req, res) {
109+
req.session.loggedIn = null;
110+
res.redirect('/');
111+
});
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{
2+
"name": "mongoose-example",
3+
"version": "0.0.1",
4+
"dependencies": {
5+
"express": "2.5.2",
6+
"mongoose": "2.5.10"
7+
}
8+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
<!DOCTYPE html><html><head><title>MongoDb example</title></head><body><h1>My first MongoDB app</h1><hr></body></html>
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: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
<!DOCTYPE html><html><head><title>MongoDb example</title></head><body><h1>My first MongoDB app</h1><hr><p>welcom new visitor!</p><ul><li><a href="/login">Login</a></li><li><a href="/signup">Signup</a></li></ul></body></html>
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: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
<!DOCTYPE html><html><head><title>MongoDb example</title></head><body><h1>My first MongoDB app</h1><hr><form action="/login" method="POST"><fieldset><legend>Log up</legend><p><label>Email</label><input name="user[email]" type="text"></p><p><label>Password</label><input name="user[password]" type="text"></p><p><button>Submit</button></p><p><a href="/">Go back</a></p></fieldset></form></body></html>
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: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
<!DOCTYPE html><html><head><title>MongoDb example</title></head><body><h1>My first MongoDB app</h1><hr><form action="/signup" method="POST"><fieldset><legend>Sign up</legend><p><label>First</label><input name="user[first]" type="text"></p><p><label>Last</label><input name="user[last]" type="text"></p><p><label>Email</label><input name="user[email]" type="text"></p><p><label>Password</label><input name="user[password]" type="text"></p><p><button>Submit</button></p><p><a href="/">Go back</a></p></fieldset></form></body></html>
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)