Discover AdonisJS (MVC framework for Node)
Tags: javascript, adonis, node, express
Published: 2021-03-27 13:34:26

---
title: Discover AdonisJS (MVC framework for Node)
published: true
description: 
tags: javascript, adonis, node, expressjs
cover_image: https://dev-to-uploads.s3.amazonaws.com/uploads/articles/2r7pwf06szmde50ryv93.png
series: AdonisJS
---
Follow me on Twitter: <a href="https://twitter.com/justericchapman?ref_src=twsrc%5Etfw" class="twitter-follow-button" data-show-count="false">Follow @justericchapman</a><script async src="https://platform.twitter.com/widgets.js" charset="utf-8"></script>

<h2>What is AdonisJS?</h2>
In summary, this is the Node.js version of PHP Laravel. AdonisJS is a framework that contrasts with other Node.js frameworks. AdonisJS is a batteries included framework that is highly focused on developer ergonomics, stability and speed.

In this series we will discover AdonisJS by creating a first web app step by step. 

<h2>Requirement</h2>
To create a Adonis project the only requirement is to have Node.js install on your machine. Since Node is use a lot in dev world, good chance you already have it. 

Type this command in the terminal to check your installed Node version:
```
node --version
```
If version >= 12.0.0 you are ok. If not go to nodejs.org and install the last version. (https://nodejs.org/en/download/)

<h2>Create your first Adonis Project!</h2>
To create a new Adonis web app we run in the terminal:
```
npm init adonis-ts-app blog
```
![choose adonis app type](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/hqcv5m2obi79eti3xe6f.png)

![adonis completed](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/13m0fwrjlcbjgwvr2v1d.png)Once the project is created. Follow the on screen instruction:
```
cd blog
node ace serve --watch
```
This last command will start the server on http://127.0.0.1:3333 and watch and refresh if any change.

You can then open your browser to the specified address:
![adonis](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/sgkj41h24ye5xc4fyc42.png)

Now let's take a look at the created project. Open the newly created folder with your favorite code editor.

<h2>Project scaffolding</h2>
Adonis is a batteries included framework that is made to save you time. The framework create for you the project structure and boiler plate code that allow you to save ton of times doing it by yourself. 

We now have only to concentrate on the fun part... Start coding our solution :-)

Since it's your first time with Adonis we will discover together the folder structure:

![adonis folder structure](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/grl6u8wg45umryi1uoc4.png) 

The list could look overwhelming but don't be afraid it is way more simple than it look at first sight. Here a list of main folder location:
<table>
  <thead>
    <tr>
      <th>Entity</th>
      <th>Folders</th>  
   </tr>
  </thead>
  <tbody>
    <tr>
      <td>routes</td>
      
      <td>start/routes.ts</td>
    </tr>
    <tr>
      <td>controllers</td>
      
      <td>app/Controllers</td>
    </tr>
   <tr>
      <td>models</td>
      
      <td>app/Models</td>
    </tr>
    <tr>
      <td>migrations</td>
      
      <td>database/migrations</td>
    </tr>
    <tr>
      <td>views</td>
     
      <td>resources/views</td>
    </tr>
  </tbody>
</table>

<h2>Your First Step into Adonis</h2>
When we first open the new app in our browser the framework show us a default welcome page:
![adonis](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/sgkj41h24ye5xc4fyc42.png) 
How does the framework show that page? How can I replace that page with my custom page?

First let's find how that default page is display.

When searching for a specific page, it's a good idea to start in the routes file. So open your app routes file located in start/routes.ts
![routes.ts](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/1li21dbzp2et4rzt02bl.png)

```js
import Route from '@ioc:Adonis/Core/Route'

Route.on('/').render('welcome')
```
That code is a good AdonisJS code example: simple and clean. 
So when the browser hit root path '/' render the 'welcome' view.

If we look into the view folder (under resources/views) we would see a file name welcome.edge:
![welcome.edge](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/44b5imnt6my9s3bijgg1.png)
This file is an Edge Template. Edge is the powerful templating engine use by Adonis to render views.

You can change whatever you want on the page and your modifications will be render to the browser. (If your server is still running)

<h2>Create our own page with data</h2>
That welcome page is cute but a real web app will often have data query from a database and render on a page. Let's do that step by step.

First to use a database we need to install the Adonis database module.
<h4>Install Adonis database module</h4>
Installing that module is easy. Here the CLI commands to install and run first config:
```
npm i @adonisjs/lucid@alpha
node ace invoke @adonisjs/lucid
```
The invoke command will ask for the Database type. For this project choose SQLite.

<h2>Project CLI commands</h2>
Adonis is allergic to boiler plate. Creating base config for a new element can be time consuming (and boring).

That why Adonis have a CLI tool call ace. ace CLI will scaffold element for you. For example if we want to create a migrations there is a command for that. Same for models and controller.

Here a list of main CLI commands:
<table>
  <thead>
    <tr>
      <th></th>
      <th>Adonis.js</th>  
   </tr>
  </thead>
  <tbody>
    <tr>
      <td><small>create model</small></td>
   
      <td><small>node ace make:model Post</small></td>
    </tr>
    <tr>
      <td><small>create controller</small></td>
      
      <td><small>node ace make:controller Post</small></td>
    </tr>
   <tr>
      <td><small>create migrations</small></td>
     
      <td><small>node ace make:migration post</small></td>
    </tr>
    <tr>
      <td><small>Run migrations</small></td>
    
      <td><small>node ace migration:run</small></td>
    </tr>
  </tbody>
</table>

We will use those commands to create our database model.

<h2>End of part 1, tomorrow part 2</h2>
That's it for today. Stay tune for part 2 tomorrow. We will create our database and query data to a page view. 

The best way to miss nothing is to follow me on Twitter: <a href="https://twitter.com/justericchapman?ref_src=twsrc%5Etfw" class="twitter-follow-button" data-show-count="false">Follow @justericchapman</a><script async src="https://platform.twitter.com/widgets.js" charset="utf-8"></script>