Follow me on Twitter: Follow @justericchapman
What is AdonisJS?
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.
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:
If version >= 12.0.0 you are ok. If not go to nodejs.org and install the last version. (https://nodejs.org/en/download/)
Create your first Adonis Project!
To create a new Adonis web app we run in the terminal:
npm init adonis-ts-app blog
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.
Now let's take a look at the created project. Open the newly created folder with your favorite code editor.
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:
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:
Your First Step into Adonis
First let's find how that default page is display.
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.
You can change whatever you want on the page and your modifications will be render to the browser. (If your server is still running)
Create our own page with data
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.
Install Adonis database module
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.
Project CLI commands
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:
|create model||node ace make:model Post|
|create controller||node ace make:controller Post|
|create migrations||node ace make:migration post|
|Run migrations||node ace migration:run|
We will use those commands to create our database model.
End of part 1, tomorrow part 2
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: Follow @justericchapman