AdonisJs — A Laravel-ish Node Framework

As a web application developer, I mostly work using PHP and Laravel framework as a back end developer and use Javascript for front end, mostly jQuery and other micro libraries. Also, love to code using Angular. So, I’ve a good understanding of Javascript as well but never use it for back end. Well, as Node provides us the ability to use Javascript to write web application which runs on server. This is really awesome and I’ve played with it using Express framework.

Node.js is a JavaScript runtime built on Chrome’s V8 JavaScript engine. Node.js uses an event-driven, non-blocking I/O model that makes it lightweight and efficient. Node.js allows the creation of Web servers and networking tools using JavaScript and a collection of “modules” that comes with Node (written in javascript) to handle the various core functionality. So, it’s a platform where we can run web applications written in Javascript.

So, as a Node framework, Express is very popular one and most of the Node developers use it and I’ve tried it as well. It’s really nice. The most used technologies to build a web application by the Javascript developers are Mongo (NoSql Database), Express (Javascript Back End Framework), Angular (Javascript Front End Framework) and Node (JavaScript Web Development Platform) and these are known as MEAN stack tools or who uses these technologies known as MEAN stack developers. Anyways, it’s not a new concept and I’m not going to write how to develope web application using Node or MEAN stack tools. Instead, I’m writing (introductory) about another awesome framework for writing Node applications.

Before I write about the framework, let’s check the following code:

// app.js var http = require('http'); http.createServer(function(req, res) { res.writeHead(200, {'content-type':'text/plain'}); res.end('Hello World!\n'); }) .listen(3000); console.log('Running Node Server...');

Well, this is a very basic example of a Node server. Now, to run the server all I need to run the following command from the command line/terminal

node app

This will start the server on port 3000 and I can navigate the app from my browser using http://localhost:3000 which will show “Hello World!” on the browser. Alternatively, using Express framework a very basic example may look something like this:

var express = require('express'); var app = express(); app.get('/', function (req, res) { res.send('Hello World!'); }); app.listen(3000, function () { console.log('Example app listening on port 3000!'); });

Well, these are very basic examples to clerify the idea if you are not familiar with these technologies but that’s enough. If you are interested then you may read documentations and lots of useful tutorials all over the web to learn more about these. So, now, let’s talk about the AdonisJS.

As I used the word Laravel-ish so if you are familiar with Laravel framework then you will get the idea. So, at first, let’s check the following code written using AdonisJS

const Route = use('Route'); Route.get('/', function * () { // handle request });'/', function * () { // handle request });

This is a route declaration in AdonisJS framework but this is not all but you may use something like this as well, for example:

const Route = use('Route'); Route.get('/users', 'UserController.index'); // Named Route Route.get('/users/profile/:id', ... ).as('profile') // Generate the URL for previous named route Route.url('profile', {id:1}); //

Also, you can define a RESTful resource by using the resource method, for example:

Route.resource('users', 'UsersController'); // This is equals to // Route.get('users', 'UsersController.index').as('users.index') // Route.get('users/:id', '').as('') // Route.get('users/create' 'UsersController.create').as('users.create') // Route.get('users/:id/edit', 'UsersController.edit').as('users.edit') //'users', '').as('') // Route.put('users/:id', 'UsersController.update').as('users.update') // Route.patch('users/:id', 'UsersController.update') // Route.delete('users/:id', 'UsersController.destroy').as('users.destroy')

This is an example of optional route parameter, notice the ? to make it optional:

Route.get('/make/:drink?', function (request, response) { const drink = request.param('drink', 'coffee'); response.send(`I will make ${drink} for you`); });

What about route grouping, prefixing and route middleware? It’s possible:'name', function () { // all routes under this callback are part of this group });'version1', function () { Route.get('/', ...); }).prefix('/v1')'blog', function () { Route.get('/post', ...); }) .middlewares(['auth']);

Well, this is only the routing part but there are so many other things that looks really cool and give me the exact feeling of coding using Laravel but only difference is that, this is Javascript. So, that’s why I used the word Laravel-ish and if you are familiar with that framework then you’ve already realized that, I’m not wrong. Anyways, I’m just writing an introductory article here so probably this is not the best place to learn AdonisJS so please check the documentation for more details. Before I finish I would like to show you some more examples, so let’s check it out.

Let’s see how can we create a controller in AdonisJS:

Originally published at

One clap, two clap, three clap, forty?

By clapping more or less, you can signal to us which stories really stand out.