Chapter 3. API with MongoDB and Node.js

This chapter will focus directly on the code, so we can have the necessary understanding to build a solid foundation for our API. Our main aim is to discuss the techniques to build rich web applications, with the SPA approach.

Application Programming Interface (API) is the set of programming standards that enables us to build applications. Their usage is not so evident to the user using the application. The API is the engine of the application; an interface that runs behind it all. While you might benefit from an application or website, your API can be connected to several other systems and applications. With APIs, applications talk to each other without user intervention.

We will be covering the following topics in this chapter:

  • The working of an API
  • Boilerplates and generators
  • The speakers API concept
  • Creating the package.json file
  • The Node server with server.js
  • The model with the Mongoose schema
  • Defining the API routes
  • Using MongoDB in the cloud
  • Inserting data with the Postman Chrome extension

The working of an API

An API works through communication between different codes, thus defining specific behavior of certain objects on an interface. That is, the API will connect several functions on one website (such as search, images, news, authentications, and so on) to enable it to be used in other applications.

Operating systems also have APIs, and they still have the same function. Windows, for example, has APIs such as the Win16 API, Win32 API, or Telephony API, in all its versions. When you run a program that involves some process of the operating system, it is likely that we make a connection with one or more Windows APIs. To clarify the concept of an API, we will give go through some examples of how it works.

On Windows, it works on an application that uses the system clock to display the same function within the program. It then associates a behavior to a given clock time in another application, for example, using the Time/Clock API from Windows to use the clock functionality on your own application.

Another example, is when you use the Android SDK to build mobile applications. When you use the device GPS, you are interacting with the API (android.location) to display the user location on the map through another API, in this case, Google Maps API.

The following is the API example:

The working of an API

When it comes to web APIs, the functionality can be even greater. There are many services that provide their code, so that they can be used on other websites. Perhaps, the best example is the Facebook API. Several other websites use this service within their pages, for instance a like button, share, or even authentication.

Tip

An API is a set of programming patterns and instructions to access a software application based on the Web.

So, when you access a page of a beer store in your town, you can log in with your Facebook account. This is accomplished through the API. Using it, software developers and web programmers can create beautiful programs and pages filled with content for their users.

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

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