The development team at DA-14 designed and built APIs for various domains, like Engineering & Construction, Tourism & Travel, and so on. Over the years, we’ve learned useful lessons and defined some best practices for creating APIs easily and quickly.
The majority of companies encounter same problems, building own RESTful APIs from the ground up. For instance, applying Express is time-consuming, because it requires stepwise design and setup of each separate model with parameters, security, endpoints, etc. In this connection, LoopBack quickly gains traction taking the pain out of an API development process and reducing engineering time.
LoopBack advantages for API development:
- Built on the top of Express.js
- Model-based
- Quick scaffolding of dynamic RESTful APIs with Yeoman
- Out-of-the-box compatibility with multiple databases, like PostgreSQL, MySQL, MongoDB, etc.
- Integrated user authentication with oAuth and social logins through Passport.js
- Swagger compliancy
- Easy deployment and scalability
Installing LoopBack:
(note that –g stands for “global” and its usage isn’t essential)
- Proceed with building a new application. Run slc loopback to start and choose your project name
- This will generate all the core files/code for your new API automatically. For test use new directory and run node. API Explorer at http://localhost:3000/explorer allows exercising API endpoints and testing operations.
Further steps depend on application type, chosen database, and other factors. As an example we’d like to share API development for one of our marketplace solutions in Travel & Tourism industry.
-
Go ahead with tapping to ORM:
npm install loopback-datasource-juggler --save
-
Link up to connector for PostreSQL:
npm install loopback-datasource-postgresql --save
-
Connect to preferred database:
{
"db": {
"name": "db",
"connector": "postgresql",
"database": "dbname",
"username": "dbuser",
"password": "dbpassword"
}
}
Why LoopBack is the best choice?
- Highly-adaptable and open source framework — vast library of Node.js modules that can be used all together or independently;
- Fine-structure security — LoopBack allows setting up access data for an app, defining roles and customizing access control lists (ACLs). Access control modules work right out of the box; moreover, LoopBack provides support for social logins through Passport.js;
- Deployment and scalability — to facilitate the deployment process and to seize the opportunities of server space all Node apps can be scaled to multiple hosts or multiple processes on a single host. Such vertical/horizontal scaling or simply clustering ensures zero app downtime and works right out of the box with no coding;
- Swagger 2.0 compliancy ensures interactive documentation, readability and client SDK generation. Swagger features the largest stack of API toolchain, while it is an easy and still powerful representation of REST API;
- API versioning facilitates future updates and streamlines further support. You may not upgrade your app right away, but it is a #1 concern while designing your API. Versioning prevents existing apps from breakage when any changes are made, for instance renaming or deleting a parameter can lead to the risk of bringing down many or all the clients’ systems;
- Compatibility with dozens of SQL and NoSQL data-sources including MongoDB, SQL Server, MySQL, Oracle and others. API Platform also facilitates connection to various messaging, services and big data backends, eliminating the need to write joins, logic or additional code;
- LoopBack enables gathering and viewing performance analytics via built-in Arc Tool module. Arc provides tools for monitoring, configuring and building Node applications. Furthermore, it allows developers to find bottlenecks and trace execution paths;
- Logging is crucial for auditing and debugging apps, for instance during the production stage log rotation can facilitate handling and storage of log files and save server space. We mainly use Winston with its large set of features and easy-of-use. This logging library enables developers to: use various transports or create custom ones; stream/query logs; perform profiling and handle exceptions; use predefined error levels or create custom ones;
- LoopBack allows you to control the amount of API requests that can be made within a limited period of time. This is better known as rate limiting.
With LoopBack you are apt to write less code, compared to Express.js and similar frameworks. Apart from that it eliminates the need to write handlers for every endpoint, supports filters for easy search and pagination, stores code in separate model JS files, much more. Among the top advantages are prompt and easy generation, design and test of code for APIs. Such approach allows developers to mainly focus on user experience and business logic, and therefore build secure, well-documented, and high-performance APIs.