Creating a RESTful API with Node.JS
Most of the people using APIs nowadays don’t even know what it means and that’s perfectly normal. Their purpose is to simplify how you get data from the internet. You create your own service that serves data in a nice format (mostly JSON, nowadays) to whoever wants to use it. It’s a pretty simple concept and we’re going to show you that is pretty simple to create one also.
We’re going to use LoopBack to create our API, mostly because it simplifies the whole process. LoopBack is a framework that asks us for the costumizable values (such as names) and does the rest. Keep it simple, you know?
You should have Node.JS and a bash terminal installed, both of which you can learn how install on my last article. The first thing we’re going to do is create an empty folder and navigate into it using our terminal.
Installing LoopBack is as easy as writing:
npm install -g loopback-cli
Creating our project can now be done using the following command:
It may take a little, but it should prompt us to give our project a name. Mine will be loopback_api. Using the arrow keys to select, choose version 2.x and api-server.
Next step is to create a model. You could guess, but that’s easy aswell. Just type:
It will give you some choices, but we mainly click Enter and go forward. I choose a model name of loopback_model, with no datasource, a standart model, no custom plural form and common for access everywhere. Added a property called nombre of type string, required with no default. And we’re set!
What’s left for us to do is just running our server. Just type:
Open your browser and go to the url:
You’ve just built your own API! If this is so simple, what are kids studying for?