-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
149 lines (131 loc) · 3.12 KB
/
app.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
/**
* created by maning
*/
var express = require('express')
, mongodb = require('mongodb')
, bodyParser = require('body-parser')
, cookieParser = require('cookie-parser')
, session = require('express-session');
// get the IP and PM2.5
var logInfos = require('./public/modules/module_getIpAndPm25');
var logInfo = logInfos.getIpAndPm25();
// get the products from Tmall
var products = require('./public/modules/module_getProudctTmall');
var productList = products.getProduct();
/**
* create the app
*/
app = express();
/**
* set view item
*/
app.set('view engine', 'jade');
/**
* set static path
*/
app.use(express.static('views'));
app.use(express.static('public'));
/**
* set the middleware
*/
app.use(bodyParser.urlencoded({ extended: true }));
app.use(bodyParser.json());
app.use(cookieParser());
app.use(session({
resave: false,
saveUninitialized: true,
secret : 'my secret'
}));
/**
* authenticated check
*/
app.use(function (req, res, next) {
if (req.session.loggedIn) {
var objectID = mongodb.ObjectID;
res.locals.authenticated = true;
app.users.findOne({ _id: objectID(req.session.loggedIn) }, function (err, doc) {
if (err) return next(err);
res.locals.me = doc;
next();
});
} else {
res.locals.authenticated = false;
next();
}
});
/**
* default route
*/
app.get('/', function (req, res) {
res.locals.loggedCity = logInfo.loggedCity;
res.locals.loggedPm25 = logInfo.loggedPm25;
res.locals.productList = productList;
res.render('index');
});
/**
* login route
*/
app.get('/login', function (req, res) {
res.render('login');
});
/**
* logined route
*/
app.get('/login/:signupEmail', function (req, res) {
res.render('login', { signupEmail : req.params.signupEmail });
});
/**
* login post route
*/
app.post('/login', function (req, res) {
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.send('<p>User not found. Go back and try again.</p>');
req.session.loggedIn = doc._id.toString();
res.redirect('/');
});
});
/**
* signup route
*/
app.get('/signup', function (req, res) {
res.render('signup');
});
/**
* signup post route
*/
app.post('/signup', function (req, res, next) {
app.users.insertOne(req.body.user, function (err, doc) {
if (err) return next(err);
res.redirect('/login/' + doc.ops[0].email);
})
});
/**
* logout route
*/
app.get('/logout', function (req, res) {
req.session.loggedIn = null;
res.redirect('/');
});
/**
* connect to db
*/
var MongoClient = mongodb.MongoClient;
var DB_CONN_STR = 'mongodb://localhost:27017/test';
MongoClient.connect(DB_CONN_STR, function(err, db) {
if (err) throw err;
app.users = db.collection('maning');
console.log('\033[96m + \033[39m connected to mongodb');
db.ensureIndex('users', 'email', function (err) {
if (err) throw err;
db.ensureIndex('users', 'email', function (err) {
if (err) throw err;
console.log('\033[96m + \033[39m ensered indexes');
});
});
});
/**
* listener
*/
app.listen(3000);