Node.js is one of the emerging technologies to write real-time applications using one of your favorite web languages: Javascript. With the ample choices of frameworks to write your first web server, not even a single one offers the desired developer experience. This is where AdonisJs shines.

AdonisJs is a beautifully crafted MVC framework for Node.js. In this article I'll show you how to get started with AdonisJs by installing the framework, discussing the features and creating a welcome page.

AdonisJs is inspired by a Php Framework called Laravel. It borrows the concepts of Dependency injection and service providers to write beautiful code which is testable to its core.

In the age of shiny web frameworks, AdonisJs focuses on the key aspects of creating stable and scalable web applications, some of them are:

Developer Experience

The framework makes use of latest inbuilt ES2015 features to get rid of spaghetti code. You will hardly find yourself writing callbacks since there is a solid support for ES2015 generators. Also, the OOP nature of the framework helps you in abstracting your code into multiple re-usable chunks.

Consistent API

The API throughout the code base is so consistent that after working for a while, you will be able to guess the method names, expected output, etc.

Speed & Productivity

AdonisJs ships with a bunch of 1st party components known as providers. Writing entire web server is a matter of weeks(if not days). It ships with solid support for Mailing, Authentication, Redis, SQL ORM, Data validation and sanitization, etc.

The installation process is straight forward. Just make sure you have got versions of node >= 4.0 and npm >=3.0 .

Let's start by installing adonis-cli globally using npm. It is a command line tool to scaffold new applications within a matter of seconds.

npm i -g adonis-cli

Now we can start by creating new projects. For this article, we will setup a project called hello-adonis .

adonis new hello-adonis

Above command will setup a new project with the pre-configured directory structure. Don't get overwhelmed by the directory structure as in next article we will talk about it.

Now cd into the newly configured project and executing the following command to start the HTTP server.

cd hello-adonis npm start

Open http://localhost:3333 to see the welcome page.

In this article, we will discuss the framework concepts and some exciting features and in the next article, we will have fun by writing some code.

AdonisJs has out of the box support for defining fluent routes. Setting up express style routes to CRUD resources is a just a matter of seconds.

Let's explore the framework routing layer.

const Route = use ( 'Route' ) Route . get ( '/home' , 'HomeController.welcome' )

Above we defined a simple route to URL /home . Once the given route is called the welcome method on HomeController will be invoked.

Let's move to my favorite part of routing called resources . Resources help you in defining multiple conventional RESTful routes within a single line of code.

const Route = use ( 'Route' ) Route . resource ( 'users' , 'UserController' )

Above line of code will set a total of seven routes.

Url Verb Controller Method Purpose /users GET index Show list of all users /users/create GET create Display a form to create a new user. /users POST store Save user submitted via form to the database. /users/:id GET show Display user details using the id /users/:id/edit GET edit Display the form to edit the user. /users/:id PUT/PATCH update Update details for a given user with id. /users/:id DELETE destroy Delete a given user with id.

That's not all. You can also filter and extend resources based upon the nature of the application.

const Route = use ( 'Route' ) Route . resource ( 'users' , 'UserController' ) . only ( [ 'index' , 'store' , 'update' , 'destroy' ] ) . addMember ( 'profile' )

Whoooo! As I said, the fluent method chaining makes it so simple to filter the resources down to only four routes, and also with the help of addMember , we can add a new route to the users resource.

Here's a neat video to show off some more routing features:

Route Middleware

Before we move onto the next feature, let's see how to work with route middleware.

Route . get ( '/account' , 'UserController.account' ) . middleware ( 'auth' )

Middleware(s) are defined by chaining the middleware method, and you are free to specify one or more middleware by passing multiple arguments.

If you ever worked with Rails or Laravel, you will feel right at home. AdonisJs ORM called Lucid is a robust implementation of Active Record. It supports all popular SQL databases like MYSQL, Oracle, PostgreSQL, SQLite, etc. Also, the database engine has support for data models, fluent query builder, and migrations.

Let's take a quick look at how to define and interact with SQL data models also known as Lucid models.

const Lucid = use ( 'Lucid' ) class User extends Lucid { posts ( ) { return this . hasMany ( 'App/Model/Post' ) } }

Now you can use the Post Model as follows.

const User = use ( 'App/Model/User' ) yield User . all ( ) const loue = yield User . findBy ( 'username' , 'Loue' ) yield loue . posts ( ) . fetch ( )

There's hardly any Node.js framework which gives you the power to write project-specific terminal commands with the ease as similar to AdonisJs. AdonisJs has a beautiful tool called Ace , which makes it super easy and intuitive to write your terminal commands. Don't believe me! Let's check it out.

Commands are stored in the app/Commands directory, and each file represents a single command. Let's play with the pre-existing Greet command.

Run the following command from the root of your project.

./ace greet "Your name."

It is the simplest command you can ever write, but that does not limit you from creating more useful commands. For now, we will discuss the basic structure of a command class. Whereas you must reference the offical documentation.

const Command = use ( 'Command' ) class Greet extends Command { get signature ( ) { return 'greet {name}' } get description ( ) { return 'Greet a user with their name.' } * handle ( args , options ) { } }

Command signature defines the command name and the required/optional arguments to be passed when running the command. The description gets displayed on the help screen. handle is an ES2015 generator method, which is called when your command is invoked. You are free to do almost anything inside this method.

There is a lot more to cover when it comes to AdonisJs features. Let's sum them within a small list to keep this first article simple and easy to digest.

Multi-transport mailer. Robust middleware layer to interact with incoming HTTP requests. Nunjucks based templates. Support for emitting and listening to application-wide events. Inbuilt support for Redis. Secure and straightforward file uploads. Security features with support for CORS, CSRF, Content sniffing and XSS attacks. Supportive and friendly community.

AdonisJs is under active development with the latest release of version 3.0. Major API specs have finalized so that upcoming releases will have less or no breaking changes.

Also, there is a complete roadmap on Trello sharing what's next in the queue. To summarize, you will soon get support for:

Social Authentication via Facebook, Google, Github, etc. Complete testing framework. Support for caching database queries and views fragments. Inbuilt support for posting messages to Slack.

This post is an introduction to AdonisJs, walking through some of the fundamentals and key features of the framework. In the upcoming post, we will together write a simple link sharing website like EchoJs.