Home

2 minute read

AdonisJS for Beginners

Tony Lea

AdonisJS for Beginners

AdonisJS is a Node framework that feels a lot like Laravel. In fact, many of the features feel and act very similar to Laravel.

In this quick tutorial, I'll show you how to get set up with a simple Adonis application and create a few routes. Let's begin.

Installation

Installing Adonis is as simple as installing an npm package. Make sure that you have NPM installed on your machine, and then you can run the following command to install Adonis:

npm i -g @adonisjs/cli

You will now have the Adonis CLI installed on your machine. You can run the following command to make sure that it's installed.

adonis --help

And you'll see an output of all the helper options available. Next, we will create a new Adonis application.

Create a New App

Using the Adonis CLI, we can create a new application with the following command:

adonis new application

Replace the application name with the folder name you want your new Adonis application created in. Next, we can go into that folder:

cd application

And run the following command to serve up our new app:

adonis serve --dev

You should see an output that tells you your new Adonis application is being served up on your local machine.

adonis-new.png

You’ll now be able to visit http://127.0.0.1:3333, and you'll see a new Adonis application in front of you.

adonis-app.png

Creating a new Route

The route file is located inside your new project folder at start/route.js , open this new file, and you will see the following contents:

'use strict'

/*
|--------------------------------------------------------------------------
| Routes
|--------------------------------------------------------------------------
|
| Http routes are entry points to your web application. You can create
| routes for different URL's and bind Controller actions to them.
|
| A complete guide on routing is available here.
| http://adonisjs.com/docs/4.1/routing
|
*/

/** @type {typeof import('@adonisjs/framework/src/Route/Manager')} */
const Route = use('Route')

Route.on('/').render('welcome')

We can add a new route to the end of this file by adding the following line:

Route.get('hello', () => 'Hello Adonis')

Now, if we visited our application at http://127.0.0.1:3333/hello, we would get a simple "Hello Adonis" displayed on the screen.

This is just a quick example of creating a simple route that outputs text; when creating routes, you will typically map it to a Controller that renders a view. Be sure to learn more over at the Adonis documentation.

Conclusion

Adonis is a really cool framework with a lot of potential. The structure and core concepts look very much like some of the most popular web frameworks, like Rails and Laravel.

Be sure to check out this awesome Node framework to see if it's a good fit for your next great idea 🙌