Chapter 8. Creating a RESTful API

Now that your application is complete and ready for the world to see, you might start thinking of ways to make it more popular. What if you wanted to allow external systems with access rights to your data in a way that they could mass produce inserts to your website without the need for users to visit the actual website? One example that comes to mind almost immediately is suppose users of another website, say www.facebook.com, can upload an image to Facebook and have it automatically uploaded to your website as well; can this be done?

The only way to make a scenario like this even possible is by providing an API to your data and code that gives external developers access to a suite of tools that will allow them to perform actions without the need to interact with the actual web pages.

In this chapter, we will review the following topics:

  • Introducing RESTful APIs
  • Installing a few basic tools
  • Creating a basic API server and sample JSON data
  • Responding to GET requests
  • Updating data with POST and PUT
  • Removing data with DELETE
  • Consuming external APIs from Node

What is an API?

An Application Programming Interface (API) is a set of tools that a computer system makes available that provides unrelated systems or software the ability to interact with each other. Typically, a developer uses an API when writing software that will interact with a closed, external, software system. The external software system provides an API as a standard set of tools that all developers can use. Many popular social networking sites provide developer's access to APIs to build tools to support those sites. The most obvious examples are Facebook and Twitter. Both have a robust API that provides developers with the ability to build plugins and work with data directly, without them being granted full access as a general security precaution.

As you will see with this chapter, providing your own API is not only fairly simple, but also it empowers you to provide your users with access to your data. You also have the added peace of mind knowing that you are in complete control over what level of access you can grant, what sets of data you can make read-only, as well as what data can be inserted and updated.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.145.76.250