-
Notifications
You must be signed in to change notification settings - Fork 0
/
nodejs_server.js
68 lines (55 loc) · 1.52 KB
/
nodejs_server.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
var restify = require('restify');
var app = restify.createServer({
name: 'TravMsg_REST'
});
var ArticleProvider = require('./userprovider-mongodb').UserProvider;
app.use(restify.CORS());
app.use(restify.queryParser());
app.use(restify.bodyParser());
var userProvider = new UserProvider('localhost', 27017);
// Routes
app.get('/', function(req, res) {
res.send('Hello World');
});
app.get('/user/:id', function(req, res) {
});
app.get('/callback', function(req, res) {
var access_token = req.query.access_token;
var refresh_token = req.query.refresh_token;
userProvider.save({
'refresh_token': refresh_token,
}, function( error, docs) {
if (error) {
res.redirect('/')
} else {
//Store tokens
res.header('Location', 'http://localhost:9000/callback?access_token=' + access_token);
res.send(302);
}
});
});
app.post('/login/google', function(req, res) {
var access_token = req.access_token;
var googleId = req.google_id;
userProvider.find({
'access_token': access_token,
'google_id': google_id
}, function( error, docs) {
if (error) {
res.redirect('/');
} else {
res.header('Location', 'http://localhost:9000/callback?access_token=' + access_token);
res.send(302);
}
});
});
app.post('/test', function(req, res){
userProvider.save({
title: req.param('title'),
body: req.param('body')
}, function( error, docs) {
res.redirect('/');
});
});
app.listen(3000);
console.log("Express server listening on port %d", app.address().port);