The internet of today is built on web APIs. If you are unfamiliar with the concept you should check out the definition on Wikipedia. In essence, APIs facilitate the creation and consuming of data. Most of them are RESTful and use JSON.
There are many different ways to code an API. A backend could be PHP or Node.js/Express or .NET WebAPI or a number of other implementations. Data is stored in a persistance layer, usually a relational database, and there are just as many flavors of DBs as there are backend solutions.
This got me thinking about a challenge I could set for myself: Develop the same API in as many different backends as possible with my current knowledge. Here are the broad strokes:
An imaginary company named Book-Bound is looking to create an internal-use application & API for managing books and authors. They have asked you, their lead developer, to design a prototype with multiple backends so they can decide on the technology they would like to use.
- Create a RESTful API that provides JSON
- The API should have CRUD endpoints for books and authors
- The base URL should be http://localhost:3000/api
- No security, authorization or SSL is necessary
For the frontend application I am going to use Angular CLI and Bootstrap 4, specifically ng-bootstrap. Ng-Bootstrap removes my dependency on jQuery and popper.js while gaining access to some nice features like TypeAhead. Using Angular gives me a chance to use my new ng-idle libraries for reactive forms and http services.
Initially, for frontend development, I could use the angular-in-memory-web-api but it has the limitation of running on the same url base as your application. For example, you can only make calls to ‘api/books’ not ‘http://localhost:3000/api/books’. I think I might go this route and store the endpoint url in an environment variable. That way I could run the application on StackBlitz.
Using the angular-in-memory-web-api might be an after thought. For more rapid development I will probably use JSON Server. It will run on a specified port and is simple to set up. Just point it at a JSON file and it handles everything. A quick look through the code and you can see that it is Node.js/Express under the hood. No matter how it works, json-server will be our first official backend API.
Then I will create additional APIs using Node.js/Express & MongoDB, ASP.NET Core 2 & SQL Server LocalDB, and finally ASP.NET MVC5/WebAPI & either LocalDB or SQLLite. Both ASP.NET projects will use Entity Framework DbMigrations for easy db setup and change tracking.
So that will be my answer to the challenge. An Angular frontend that doesn’t care where the data is being persisted, and APIs built on the following technology:
- JSON Server & JSON files
- Node.js/Express & MongoDB
- ASP.NET Core 2 & SQL Server LocalDB
- ASP.NET MVC5/WebAPI & SQLLite
All code will be available on my GitHub. I will update this post with links to posts about each implementation.