⚠️ This lesson is retired and might contain outdated information.

Launch Twitter Client in Express Server

Share this video with your friends

Social Share Links

Send Tweet
Published 4 years ago
Updated 2 years ago

In this lesson you will learn how to use the tokens from twitter app in your twitter client to make the first call to the twitter api and verify that everything is set up correctly.

Dimitri Ivashchuk: [0:00] We are now in Node/Express project. As you see, we have express dependency. Then, we have nodemon to automatically reload our server on changes. We have dotenv dependency to manage our environmental variables. Finally, we have twitter library, which eases the communication with Twitter API.

[0:24] In our index.js in src folder, we are booting up our express server and launching the app where we will be using our Twitter methods. Our .env file looks like this. We have our port, and then our TWITTER_KEY, SECRET, access TOKEN, and access TOKEN_SECRET. With this, we are ready to go and boot up our Twitter client.

[0:50] We can go to the twitter-index.js file. Here, we have imported dotenv to manage our environmental variables. Let's import them with the structure all they needed, TWITTER_KEY, SECRETS, and TOKENS from process.env.

[1:08] Now, we can require Twitter from Twitter package, so const Twitter = require('twitter'). Finally, we want to instantiate the client. The client is just the new instance of Twitter class to which we pass all the correct keys.

[1:34] We are ready to implement our custom method, which we will use to test if the whole stuff is working. getRateLimit is a method which will return you the Twitter rate limiting on your particular application. What it does, it's an async call to Twitter API to which you pass the route. In this case, it's application/rate_limit_status route.

[2:03] We are ought to pass the empty object as we don't want to get any options to this query. We just return the rateLimit result. Finally, we export getRateLimit function to use it later in index.js. Let's do it.

[2:23] Here, the first one to import it, const {getRateLimit} = require('./twitter'), and we want to sign the variable result with await getRateLimit() function. Finally, we want to console.log(results). Now we do yarn run dev or npm run dev to launch the application. You see that we get some response logged into our console.

[3:06] We are more or less interested in the following rate limit status, which is '/users/lookup'. We do result.resources and then in resources we want to get '/users/lookup'. Let's console.log this and you see that you get 900 lookups and it will reset the particular timestamp.

egghead
egghead

Member comments are a way for members to communicate, interact, and ask questions about a lesson.

The instructor or someone from the community might respond to your question Here are a few basic guidelines to commenting on egghead.io

Be on-Topic

Comments are for discussing a lesson. If you're having a general issue with the website functionality, please contact us at support@egghead.io.

Avoid meta-discussion

  • This was great!
  • This was horrible!
  • I didn't like this because it didn't match my skill level.
  • +1 It will likely be deleted as spam.

Code Problems?

Should be accompanied by code! Codesandbox or Stackblitz provide a way to share code and discuss it in context

Details and Context

Vague question? Vague answer. Any details and context you can provide will lure more interesting answers!

Markdown supported.
Become a member to join the discussionEnroll Today