Adonis is a web framework for Node.js which is focused on productivity and developer happiness. It is often compared to Ruby on Rails, as they both offer a similar philosophy and approach to web development.
In this tutorial, we will explore why Adonis is considered the "Rails for JavaScript" and how to get started with it. We will cover the basics of setting up a new project, routing, controllers, views, and middleware.
Setting Up a New Project
To get started with Adonis, you first need to install it. You can do this by running the following command on your terminal:
npm install -g @adonisjs/cli
Once the installation is complete, you can create a new Adonis project by running:
adonis new myapp
This command will create a new Adonis project in a directory called myapp
. You can then navigate into this directory by running:
cd myapp
Routing
Routing in Adonis works similarly to Rails. You can define routes in the start/routes.js
file. Here’s an example of a basic route:
Route.get('/', 'HomeController.index')
This route tells Adonis to call the index
method on the HomeController
whenever a GET
request is made to the root URL.
Controllers
Controllers in Adonis are responsible for handling the business logic of your application. You can generate a new controller by running:
adonis make:controller HomeController
This command will create a new controller file in the app/Controllers
directory.
Views
Views in Adonis are used to render HTML templates. You can generate a new view by running:
adonis make:view home/index
This command will create a new view file in the resources/views
directory.
Middleware
Middleware in Adonis is used to intercept and modify incoming HTTP requests. You can define middleware in the start/kernel.js
file. Here’s an example of a basic middleware:
const namedMiddleware = {
auth: 'App/Middleware/Auth'
}
This middleware will be applied to all routes defined in your application.
Conclusion
Adonis is a powerful web framework for Node.js that offers a similar experience to Ruby on Rails. In this tutorial, we covered the basics of setting up a new project, routing, controllers, views, and middleware. Hopefully, this has given you a good overview of why Adonis is considered the "Rails for JavaScript" and how to get started with it.
I absolutely love Adonis. I have hands down fallen in love with it after a year of using NestJS. Nest was a miserable pain and its DI got in my way time after time. Testing was terrible, CLI was half-assed, and it just felt like a weight. With Adonis, I fell back into my Rails-based shoes and felt at home again. I haven’t used Rails in almost 7 years since I choose the NodeJS road, and no framework could scratch that itch.
But with Adonis, every step has simply been Quality of Life. The DI barely exists, the CLI is feature rich, the Lucid database solved my Prisma woes, and the lack of decorators has been a godsend.
For reference, I built a frontend application and backend for a crisis line call center. I’ve had to tackle with Cisco Broadworks and a 500+ page black and white pdf. I’m not an expert, but from production level experience I would never chose Nest over Adonis.
Very insightful and helpful, thank you. To make sure I'm understanding the entirety of your position, you propose that JS-centric teams / devs should consider adopting Adonis; but for those not attached to JS, then Rails / Django / Laravel (or, your personal preference, Phoenix) would still be preferable. Am I understanding your position correctly? I'm a semi-n00b researching the different paths at my disposal, before I invest the time and energy.
On a related note, I'm seeing objections to Laravel claiming that its release cycle introduces breaking changes every 12 months. ("Breaking", i.e., lacking backward compatibility / inability to access new features from old code.) In this way, it seems to me that Laravel wouldn't be much different (in this regard) than the maintenance nightmare that has become the JS ecosystem. Or is this just a reality of working with web frameworks in general; i.e. that one must be committed to rewriting / refactoring code on an annual basis?
I value your insights. Peace.
Adonisjs is a amazing framework, but the fact that everything is build in house scare me as fuck. Because if Adonis die basically I will have to rewrite all my source code
After a long time fighting with js frameworks and putting together stacks, i finally gave up and went with adonis 😅 im never look back
Brasil, JS, ganhou +1 like and subscribe
What about NuxtJS?
Migueeeel we need more Miguel
oh look another one…
I guess, we should have more Miguel in future videos🐈❤
Bro, im also a daniel, software developer e brasileiro. Im impressed to find you, ngl
New sub for the cat 😺
Adonis is great, the maintainers put a lot of effort into adding new stable packages when the launch the version 6 👌 I was a bit worried due to the main mantainer is also building Medusajs, but he's keeping the quality
The only problem is that even if AdonisJS is great, companies usually don't use it. Also, I'd rather use Django, Rails or Phoenix for personal projects anyway precisely to avoid having to code in Javascript.
Ai eles fazem igual o adonis 4 KKKK
really liked your takes 🙂
I think mongodb support and built-in task scheduler are missing part in Adonis js. if we have it, it would be great
pinoy:
Thank you for these very inspiring words! I will seriously try Adonis.
You explain that we should avoid using next.js or prisma because they are maintained by a company that wants to make money, but what about Angular/Google and React/Facebook?
next.js bad, prisma bad; htmx and drizzle are the solution
What about Meteor.js? Probably the most productive I've been coding full stack as an individual.
Adonis is completely underrated!