Starting an API using javascript
I’ve been using Node.js for a decade, and I can say I’m happy with it.
It is kind of crazy to think that the latest release of “Express” was more than a year ago, and it is still being used in millions of projects. To this day, it remains the most popular framework in Node.js.
The question is, should I still use it for a brand-new API project?
To answer that, in my opinion, it really depends on the project and the team you are working with. Other counter-questions you could ask to help make the decision are:
- Is there any other project using the same structure?
- If you are thinking of using a new tool for the job, how different is it from the current structure?
- How hard will it be to start a new structure?
- Do you want to keep projects on separate structures?
The point I want to make is that it doesn’t matter which decision you make—it is a trade-off. By choosing to use a structure that hasn’t been updated for quite a while, you won’t receive the latest security updates.
As a developer, I think it’s good to have some competition in the ecosystem. However, this also comes with a downside: we are somewhat separating the ecosystem even more.
What I’m using for my projects?
Code structure
I’m creating a project using Hono, which is basically a simple web framework that follows the web standard API.
Here is a bit of the structure of the project.
You can check the repository here.
Framework agnostic?
Thanks to Hono's simplicity, you can structure your project in a way that suits your needs.
The core of this project is located in the /app directory, where I use only JavaScript; none of these files are specific to Hono. This means that if you ever need to switch away from Hono for any reason, you can simply copy the /app directory and adjust the request handling as needed.
Why hono?
Based on my experience with Express.js and Fastify, I’ve found Hono to be powerful, easy to use, and supported by an active community. If you're still not convinced, Fastify is also an excellent option.
Why this structure?
This is a matter of personal preference and depends on your application and deployment process.
I've been using this case structure for a while and have found it enjoyable, though I'm still improving and learning as I go.
I often aim for a balanced approach to structure for various reasons.
As a personal recommendation, try not to become too attached to any one framework. You’ll gain more value by focusing on structuring your code and learning about patterns that can benefit your team, projects, and clients.
Feel free to adapt these ideas to fit your needs.
Hono best practices
Hono presets