This video deals with setting up the development environment so that we are ready to create our application.
- [Instructor] Hello and welcome to the last video of this section. In the previous video, we learned about the REST architecture. In this video, we are going to get our hands dirty. We will create an Express.js application. Express.js is a lightweight Node.js web framework. Then we will generate Twitter API keys so that we can gain access to Twitter's data. Finally, we will install MongoDB. MongoDB is a document-based database that we can use to store user data. Before we create an Express.js application, let us look at what Express.js can do for us.
As a web framework, Express.js gives us an interface for accepting HTTP requests on any port. Reading the data in HTTP requests, which includes HTTP methods, URL path, headers or bodies. Creating HTTP responses, and sending the responses back to the clients. To create an Express.js application, we first need to create a Node.js application. Let's do that. First, let's start by creating a directory for our application. We will name our directory twitter-notes. Let's go into that directory and run npm in it.
This will initialize the package.json file which has our application's metadata such as name, version, and dependencies. Having a package.json is good practice because with that, you would not have to include all the dependency to that code. We can leave the default name as twitter-notes and press Enter. Version 1.0.0 is good for now. Description. An application for leaving notes on Twitter friends and followers.
Entry point looks good. All looks good. For author, we can put our names. Okay. Now we get a summary of the package.json contents. If it looks good, we will press Enter. Now, if we look at the files in the current directory, we should see the package.json file that was added. Let's look at the content. The content looks right. Now, let's create a new file called index.json and open it using our favorite editor.
To test that Node.js is working properly, let's just print "Hello world" to the console. We will save the file and run it using Node.js. Here we got it, "Hello world." Now that we got our Node.js application running, the next step is to add Express.js. Let's go to Express.js' website, expressjs.com. We can find the command to install Express.js here. We will copy this and paste it into our terminal. The --save at the end tells npm to add Express.js as a dependency in package.json.
Let's check the files in our directory. There is a new node_modules folder. This folder has Express.js, as well as all the Node.js dependencies that our application needs. Now, let's look at package.json. We should see that a new dependencies section was added with express being the only dependency. We will now create an Express.js Hello World server. In the first two lines, we require express and create an express app. This app will let us handle any incoming requests. Here, we add a handler to get request on the slash URL data path.
We can change the word get to any of post, put, or delete. The handler function accepts two parameters, a request object and a response object. These objects are used to read HTTP requests and send back HTTP responses. You will send the plain text "ho ho ho". This text will be in the response body. Finally, we run our HTTP server on port 8080 and plug a message to show that the server is running. Let's run this application and look at the response from our browser, "ho ho ho," Santa's here.
Now that we have our Express server up and running, it is time to generate Twitter API keys. To do that, let's go to apps.twitter.com. Then we sign in using our username and password. Once we sign in, we should be directed to the Twitter Apps page. We will click on Create New App. For the app's name and description, we can use any values we want. I will use Packt REST APP. App names must be unique on Twitter so you can use any name you'd like. For Description, we can use the same description that we'll use in package.json.
For Website, we can put any URL we want for now. Let's use http://www.example.com. The Callback URL field is a little tricky. Twitter claims that this field is optional. However, as of December 21st 2015, Twitter still has a bug that prevents users of the API if this field is blank. We will talk more about OAuth in section two. For now, let us put anything in this field. We can use http://www.example.com/callback.
Now that our app is successfully created, we can find our API keys under Keys and Access Tokens. We will come back to this page in section two when we discuss authentication. The last piece of the set up is the database. For this course, we will be using MongoDB as our database. To download MongoDB, let's go to mongodb.org, click Download MongoDB, and scroll down to the current stable release. Download the MongoDB for your operating system. Since I have a Mac, I will download this release.
If you use a different operating system, you can follow the installation instructions and skip to the end of this video. We move the downloaded file to our preferred installation directory. Let's create the database folder under Desktop. Now we need to extract the archive. We can do this by executing tar xzvf. We should now see the MongoDB directory. The bin subdirectory has all the MongoDB executables that we need to run. To test it, we will create a folder to hold our database data.
Let's create one here and call it db. Now, let's copy the directory's path. Now, let's go to the bin directory and run MongoDB. We will run mongod. Mongod is the database server and mongo is the database client. We will use the folder we just created as a database path. If we see the waiting for connection's message, we know that our database is running and we are ready to roll. We have now come to the end of the first section of the course.
In this section, we learned that an API is an interface that hides the details of an implementation. Then we export HTTP requests and responses and learned about the various fields that make up these requests and responses. After that, we looked at how RESTful APIs use resources to identify collections, elements, and procedures. We also learned about REST as an architecture for building scalable APIs. Finally, in this video, we created an Express.js server, generated Twitter API keys and installed MongoDB.
In the next section, we will roll up our sleeves and build an application that uses Twitter's API to read a user's friends and followers.
This Node.js course gives you an overview of a RESTful API and the logical steps of creating one. It explores three different APIs, focusing on their similarities and differences and how to effectively implement one. Instructor Saleh Hamadeh starts off by defining APIs, showing how they can be built on top of HTTP and listing the properties that make an API RESTful. Learn how to develop Twitter Notes, a sample web application that lets users leave notes for their Twitter friends. Use Twitter's API to implement a login flow and then design a web API. Additionally, get a closer look at several other real-world APIs, and learn some best practices to keep APIs secure, maintainable, and efficient.
- Identifying REST resources
- Setting up the development environment
- Consuming a RESTful API
- Creating an OAuth login request
- Getting an access token
- Saving data in MongoDB
- Building a RESTful API
- Testing user-perceived performance
- Looking at APIs in the real world
- Best practices for building RESTful APIs