Blog

  • Tech

    How to Build API with Express.js?

    July 24, 2018 — By Brain Technosys

    In order to build a secured API Design service with Express.Js, it is essential for you to know how to develop a simple Express.js API process. Furthermore, you need to lock it down through HTTP basic process of authentication as well. This includes OAuth2 Client Credentials authentication also. These are known as two basically developed authentication protocols that will allow you to construct a secured server to server API services, as a part of the API design process.

    Development of basic Auth and OAuth2

    When you are securing an API Design service, for the purpose of using it across server-side applications, you need to choose, especially between two types of authentication protocols. They are known as basic Authentication or OAuth2 Client Credentials. Here, you must know that HTTP Basic Authentication is considered very old, but also a popular protocol. The method by which it works is simple. Here, each developer is given an API key to access your service. Hence, whenever the developers make an API request, it is essential for them to send their identification and authenticate themselves along with the API key they use. Now, as a part of API design service, you must know that developing OAuth2 Client Credentials is considered as the latest protocol. It is slightly more secure when compared to that of HTTP Basic Authentication. It works in a similar way. Here, also each developer is given an API key to access your service. Furthermore, the developers make a special request to your API service, using their API key. This will enable them to get an access token temporarily for a limited period of time. The amount of time is one hour or so. After this period, it expires. During this time, developers need to send this access token to your API service to identify and authenticate. In general, if you are building an API service where security is a prime concern, use OAuth2. On the other hand, if you are building an API service, where security, as well as convenience, are important, then use Basic Auth.

    Building a Simple Express.js API

    In order to build a simple API with express.js, all that you need to do is to create a folder. In the next step, install express as well as express-stormpath. This will be used for authentication later on. Finally, create a new file under the name server.js in which you can place the Express.js code. This is a small API service exposing a single endpoint. You can test this and can get a simple return message known as JSON. You can run this API service, using node server.js.

    Setting Up of API service Stormpath

    Here, Stormpath is an API service that can store user accounts, securely. Besides, it also provides open source libraries that it easier for you to build secure websites, based on API Design. Accordingly, you can create a Stormpath account, which is one hundred percent free to set up. Further, you can create Stormpath API Key. This can be done by visiting the Dashboard page, generating an API key. In the next step, you can place the Stormpath API key into your project folder with the name, apiKey.properies. Finally, you can create a Stormpath Application for this project, API with Express.js. By following other API Design practices, you can develop other authentication procedures to test your project with Expres.js.

    Finally, it is quite simple to build with Expres.js, using API Design concept.