-
Notifications
You must be signed in to change notification settings - Fork 15.7k
/
index.js
63 lines (50 loc) · 1.21 KB
/
index.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
var express = require('../..')
, app = express();
app.set('view engine', 'jade');
app.set('views', __dirname + '/views');
function User(name) {
this.private = 'heyyyy';
this.secret = 'something';
this.name = name;
this.id = 123;
}
// You'll probably want to do
// something like this so you
// dont expose "secret" data.
User.prototype.toJSON = function(){
return {
id: this.id,
name: this.name
}
};
app.use(express.logger('dev'));
// earlier on expose an object
// that we can tack properties on.
// all res.locals props are exposed
// to the templates, so "expose" will
// be present.
app.use(function(req, res, next){
res.locals.expose = {};
// you could alias this as req or res.expose
// to make it shorter and less annoying
next();
});
// pretend we loaded a user
app.use(function(req, res, next){
req.user = new User('Tobi');
next();
});
app.get('/', function(req, res){
res.redirect('/user');
});
app.get('/user', function(req, res){
// we only want to expose the user
// to the client for this route:
res.locals.expose.user = req.user;
res.render('page');
});
/* istanbul ignore next */
if (!module.parent) {
app.listen(3000);
console.log('Express started on port 3000');
}