Reputation: 43
I am a complete newbie to Node.js, Express, and Angular. I have a Node/Express app running on my localhost
. I am trying to make this into a Twitter search application, using the Twitter API, so that I can enter a search term and the API returns the search results for this search term. For this, I am using the twitter package. In index.js
, I have filled in my Twitter keys and secrets at the Xs as follows:
var Twitter = require('twitter');
var client = new Twitter({
consumer_key: 'X',
consumer_secret: 'X',
access_token_key: 'X',
access_token_secret: 'X'
});
When I then put the following code into index.js
, the search results for keyword "awesome" are logged to the console:
var tmpSearch = 'awesome';
client.get('search/tweets', {q: tmpSearch}, function(error, tweets, response){
if (error) throw error;
console.log(tweets);
});
This works. My home page uses a controller myCtrl
, which makes sure that when the user presses the Search button (with property ng-click="search(searchTerm)"
), the entered search term is assigned to the variable searchTerm (using ng-model="searchTerm"
for the input area). The code for the controller is as follows:
app.controller('myCtrl', ['$scope',
function($scope){
$scope.search = function(searchTerm){
console.log("Searching for " + searchTerm);
// Search here...
console.log("Search finished.");
};
}
]);
It logs the search term to the console, but I don't know how to proceed from here. Where it says // Search here...
I want to execute the client.get
code from above, but I cannot use the client
variable from routes/index.js
in my public/javascript.js
. What do I need to add to my code in order to perform the search?
I have the feeling that I am not understanding a very important part of Node/Express, but I don't know which part that is, so my search for solutions hasn't been very succesful. I have never used APIs before either, and I have spent many hours going through documentation and tutorials both for Node.js and for the Twitter API, but it's only a week ago that I started learning it so most of it isn't making a lot of sense to me yet. I have found a few examples of Node apps using the Twitter API on GitHub (most of them using different packages), of which I tried to understand the code, but I couldn't figure out what I should do. I hope someone will be patient enough to explain to me what I am missing.
Upvotes: 2
Views: 2375
Reputation: 8422
You need to think about where each step is happening. The Twitter code you're showing is running in Node, on your server. The myCtrl code is AngularJS code, running in the browser. As you've sensed, there's something missing to connect them.
The flow of control will be like this:
You have pieces of this in place. What's missing is the HTTP request and response. Here's what you do:
/api/twittersearch
. You'll do this with Node.js and Expressreq
and res
(request and response; those names are not required but are frequently used); this function will do the new Twitter
and client.get
code that you have aboveclient.get
call has a callback function, which you have currently implemented; in your callback, you'll send the tweets back to the client (something like res.send(tweets)
$http.get('/api/twittersearch?term=' + searchTerm)
That last call to $http.get() returns a Promise. You'll follow that up with .then(function(tweets){ ... }).catch(function(errors){ ... })
. In the function you pass to then, you'll take the results from your call and update your model.
Upvotes: 4