-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
124 lines (98 loc) · 2.88 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
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
(function(){
'use strict';
var express = require('express'),
app = express(),
bodyParser = require('body-parser'),
data = [],
artistListHandler = function(req, resp){
console.log('artistListHandler'+req.method);
var spotify = require('./spotifysearch'),
cb = function(r, resp) {
resp.send(r);
},
params = {
query:req.body,
cb: cb,
resp: resp,
url: 'https://api.spotify.com/v1/search?'
};
spotify.search(params);
},
saveSearchHandler = function(req, resp) {
console.log('saveSearchHandler'+req.method);
var mongodb = require('mongodb'),
ObjectID = require('mongodb').ObjectID;
mongodb.connect('mongodb://localhost:27017/test2', function(err, db) {
if(!err) {
var searches = db.collection('savedSearches');
if(typeof req.body._id === 'undefined'){
searches.insert(req.body);
db.close();
}else{
var query = {_id: new ObjectID(req.body._id)};
delete req.body._id;
if(typeof req.body.deletedoc !== 'undefined'){
searches.remove(query);
db.close();
resp.send({});
}else{
searches.update(
query,
req.body,
function(err, object) {
if (err){
console.warn('Error:'+err.message);
db.close();
resp.send({dbmessage:err.message});
}else{
db.close();
resp.send({dbmessage:'success'});
}
});
}
}
}
});
},
getSearchesHandler = function(req, resp) {
console.log('saveSearchHandler'+req.method);
var mongodb = require('mongodb');
mongodb.connect('mongodb://localhost:27017/test2', function(err, db) {
if(!err) {
var query = {emailaddress:req.body.emailaddress},
searches = db.collection('savedSearches').find(query);
searches.each(function(err, item) {
if(item === null) {
db.close();
resp.send(data);
data = [];
}else{
data.push(item);
}
});
}
});
};
app.use(bodyParser.json()); // to support JSON-encoded bodies
app.use(bodyParser.urlencoded({ // to support URL-encoded bodies
extended: true
}));
app.use(express.static('web'));
app.all('/*', function(req, res, next) {
console.log(req.method);
res.header("Access-Control-Allow-Origin", "*");
res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");
res.header('Access-Control-Allow-Methods', 'GET,PUT,POST,DELETE');
next();
});
app.post('/GetSearches', getSearchesHandler);
app.post('/SaveNewSearch', saveSearchHandler);
app.put('/UpdateSearch', saveSearchHandler);
app.post('/UpdateSearch', saveSearchHandler);
app.post('/ArtistList', artistListHandler);
app.listen(3003, function () {
var host = this.address().address,
port = this.address().port;
console.log('Listening -- %s:%s', host, port);
});
})();