Hello Adonis
Take a look at a simple AdonisJs application and learn about its folder structure.
We'll cover the following
First AdonisJs application
AdonisJs, like most frameworks, follows the MVC pattern and allows developers to write consistent, stable, and expressive code. To run an Adonis application on Educative, you don’t need to set anything up! All the code will run inside a special environment within the browser.
The following is a complete implementation of a basic application in AdonisJs. Press Run to see the output. Please be patient as the server is set up for you on the fly.
'use strict' class ConvertEmptyStringsToNull { async handle ({ request }, next) { if (Object.keys(request.body).length) { request.body = Object.assign( ...Object.keys(request.body).map(key => ({ [key]: request.body[key] !== '' ? request.body[key] : null })) ) } await next() } } module.exports = ConvertEmptyStringsToNull
Folder structure
AdonisJs has an MVC structure, so we won’t have to waste our time figuring out where to put the files. It enables us to stay organized and scale files later. Let’s go over the folder structure. Don’t worry if you don’t understand some terminologies, everything will be explained throughout the course.
app
This folder contains the application logic. It contains:
- Controllers: Controllers control the flow of logic to return a response to the user. Controllers can be bound together with routes.
Note: The Controllers folder is not created by default.
-
Middleware: Middleware contains the methods that will be executed before or after an HTTP request hits the router.
-
Models: Models define the database models and relationships.
More folders like Exceptions, Validators, Helpers, ModelFilter, etc. can be created inside the app folder.
config
Our application configuration is stored here. We can also create our config file here.
database
This folder is used to store the migrations, seeds, and factories.
public
We put all our static files, like images, CSS, and JavaScript files, here. They will directly be called over HTTP.
resources
We can put all the view templates in the resources folder. We will place Edge template files here.
start
The files present here are used to load the application. We can also create files like Events, Redis, and Hooks.
.env
The .env file is located at the root folder of the application. For instance, in the .env file above, we have defined the HOST
to be 0.0.0.0
and the PORT
to be 3333
. It contains all variables related to our working environment. Development and production usually have different settings.
ace
The ace file comes in handy for executing project-specific commands.
package.json
The package.json is a standard Node.js file.
server.js
The server.js file bootstraps the HTTP server.