-
Notifications
You must be signed in to change notification settings - Fork 1
/
app.js
113 lines (93 loc) · 2.71 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
var express=require('express');
var app=express();
var path=require('path');
app.set('port',process.env.port || 3000);
app.set('views',path.join(__dirname,'views'));
app.set('view engine','jade');
var fs=require('fs');
var imagePath=path.join(__dirname,'files','image.jpg');
var users = {
'azat': {
email: 'hi@azat.co',
website: 'http://azat.co',
blog: 'http://webapplog.com'
}
};
var findUserByUsername = function (username, callback) {
// Perform database query that calls callback when it's done
// This is our fake database
if (!users[username])
return callback(new Error(
'No user matching '
+ username
)
);
return callback(null, users[username]);
};
app.get('/v1/users/:username', function(request, response, next) {
var username = request.params.username;
findUserByUsername(username, function(error, user) {
if (error) return next(error);
return response.render('user', user);
});
});
var ping=function(req,res,next){
console.log('this is ping ');
return next();
};
var pong=function(req,res,next){
console.log('this is pong');
res.end('over the song');
}
app.get('/ping',ping,pong);
app.get('/search',function(req,res){
console.log(req.query);
res.end(JSON.stringify(req.query)+'\r\n');
});
app.get('/params/:role/:name/:status', function(req,res){
console.log(req.params.role);
res.end();
});
var cookieParser = require('cookie-parser');
app.use(cookieParser('abcd'));
app.get('/cookies', function(req, res){
if (!req.cookies.counter)
res.cookie('counter', 0);
else
res.cookie('counter', parseInt(req.cookies.counter,10) + 1);
res.status(200).send('cookies are: ', req.cookies);
})
app.get('/json', function(req, res) {
res.status(200).json([{title: 'Practical Node.js', tags: 'node.js express.js'},
{title: 'Rapid Prototyping with JS', tags: 'backbone.js node.js mongodb'},
{title: 'JavaScript: The Good Parts', tags: 'javascript'}
]);
});
app.listen(1030);
app.get('/admin',function(req,res,next){
if(!req.query._token) return next(new Error('no gweg'));
},function(req,res,next){
res.render('admin');
});
//middleware that applied to all /api/* calls
app.use('/api/*',function(req,res,next){
if(!req.query.api_key) return next(new Error('No'));
});
app.get('/api/stories',findStory,function(req,res){
res.json(req.story);
});
var requireParam=function(param){
var paramName="";
if(param==='_token')
param ='token';
else if(param==='api_key')
paramName='API Key';
return function(req,res,next){
if(!req.query[param]) return next(new Error('no '));
next();
}
}
app.get('/admin',requireParam('_token'),function(req,res,next){
res.render('admin');
});
app.use('/api/*',requireParam('api_key'));