1. Home
  2. /
  3. Adonisjs
  4. /
  5. Simple Todo API

Simple Todo API - Adonisjs Typing CST Test

Loading…

Simple Todo API — Adonisjs Code

Demonstrates a simple AdonisJS controller and model for managing Todo items via REST API.

// app/Models/Todo.js
const Model = use('Model');
class Todo extends Model {}
module.exports = Todo;

// app/Controllers/Http/TodoController.js
const Todo = use('App/Models/Todo');

class TodoController {
  async index({ response }) {
    const todos = await Todo.all();
    return response.json(todos);
  }

  async store({ request, response }) {
    const data = request.only(['title', 'completed']);
    const todo = await Todo.create(data);
    return response.status(201).json(todo);
  }
}

module.exports = TodoController;

// start/routes.js
Route.get('/todos', 'TodoController.index');
Route.post('/todos', 'TodoController.store');

Adonisjs Language Guide

AdonisJS is a fully-featured, opinionated Node.js web framework for building server-side applications, APIs, and full-stack web apps with a focus on developer productivity and stability.

Primary Use Cases

  • ▸RESTful APIs and GraphQL backends
  • ▸Full-stack web applications
  • ▸Real-time apps via WebSockets
  • ▸Enterprise-scale server-side applications
  • ▸Prototyping and MVPs with structured architecture

Notable Features

  • ▸Structured MVC architecture
  • ▸Built-in ORM (Lucid) for database management
  • ▸Authentication and authorization system
  • ▸WebSocket support for real-time features
  • ▸Queues, caching, and scheduled tasks

Origin & Creator

Created by Harminder Virk in 2015, maintained by the AdonisJS core team and community.

Industrial Note

AdonisJS is often used for building full-stack Node.js applications, APIs, real-time systems, and enterprise-grade backends where consistency, structure, and developer productivity are important.

Quick Explain

  • ▸AdonisJS provides a structured MVC architecture with built-in support for routing, ORM, validation, authentication, and more.
  • ▸It emphasizes convention over configuration, offering a cohesive and batteries-included ecosystem.
  • ▸Supports synchronous and asynchronous programming via modern JavaScript/TypeScript.
  • ▸Integrates easily with relational databases, caching, queues, and WebSockets.
  • ▸Designed for both small applications and scalable enterprise-grade projects.

Core Features

  • ▸Routing and controllers
  • ▸Lucid ORM for relational databases
  • ▸Middleware and service providers
  • ▸Validation and exception handling
  • ▸Real-time WebSocket channels

Learning Path

  • ▸Week 1: Learn Node.js and TypeScript basics
  • ▸Week 2: Understand AdonisJS routing and controllers
  • ▸Week 3: Work with Lucid ORM and migrations
  • ▸Week 4: Implement middleware, authentication, and WebSockets
  • ▸Week 5: Testing, deployment, and performance tuning

Practical Examples

  • ▸Build a CRUD REST API with Lucid ORM
  • ▸Create a real-time chat application using WebSockets
  • ▸Implement JWT-based authentication and authorization
  • ▸Schedule tasks with AdonisJS queues
  • ▸Integrate third-party APIs and services

Comparisons

  • ▸AdonisJS vs Express -> AdonisJS is opinionated and batteries-included; Express is minimalistic
  • ▸AdonisJS vs NestJS -> Both TypeScript-friendly; NestJS uses decorators and dependency injection heavily
  • ▸AdonisJS vs Koa -> Koa is middleware-centric and lightweight; AdonisJS is full-featured
  • ▸AdonisJS vs Sails -> Sails is MVC and ORM-heavy; AdonisJS is modern and TypeScript-native
  • ▸AdonisJS vs LoopBack -> LoopBack focuses on APIs; AdonisJS provides full-stack capabilities

Strengths

  • ▸Structured and opinionated framework reduces boilerplate
  • ▸Batteries-included with authentication, validation, ORM, and more
  • ▸Supports TypeScript natively
  • ▸Great for enterprise apps with complex backends
  • ▸Strong conventions for maintainable code

Limitations

  • ▸More heavyweight compared to lightweight frameworks like Express or Fastify
  • ▸Opinionated structure may limit flexibility for unconventional architectures
  • ▸Smaller ecosystem than Express.js
  • ▸Learning curve for beginners unfamiliar with MVC
  • ▸Less suitable for tiny microservices

When NOT to Use

  • ▸Tiny microservices where minimal overhead is desired
  • ▸Projects requiring minimal structure or unconventional architecture
  • ▸Node.js apps that prefer lightweight frameworks like Express
  • ▸Projects where TypeScript is not desired
  • ▸Simple static sites with no backend logic

Cheat Sheet

  • ▸node ace new my-app - create project
  • ▸node ace serve --watch - start dev server
  • ▸node ace make:controller - generate controller
  • ▸node ace make:model - generate model
  • ▸node ace migration:run - run migrations

FAQ

  • ▸Is AdonisJS open-source? -> Yes, MIT License
  • ▸Does AdonisJS support TypeScript? -> Yes, natively
  • ▸Can AdonisJS handle real-time apps? -> Yes, with WebSockets
  • ▸Does AdonisJS provide built-in authentication? -> Yes
  • ▸Is AdonisJS suitable for enterprise apps? -> Yes, structured and maintainable

30-Day Skill Plan

  • ▸Master TypeScript and modern JavaScript features
  • ▸Write efficient Lucid queries and migrations
  • ▸Implement authentication, validation, and authorization
  • ▸Develop real-time features using WebSockets
  • ▸Build scalable, maintainable full-stack apps

Final Summary

  • ▸AdonisJS is a full-featured Node.js framework with MVC architecture.
  • ▸Provides ORM, authentication, WebSockets, validation, and queues.
  • ▸TypeScript-first with a batteries-included philosophy.
  • ▸Ideal for full-stack applications, APIs, and enterprise-grade backends.
  • ▸Structured conventions improve maintainability and developer productivity.

Project Structure

  • ▸app/ - controllers, models, middleware
  • ▸database/ - migrations, seeds
  • ▸start/ - routes and kernel setup
  • ▸resources/ - views and templates
  • ▸config/ - framework and database configuration

Monetization

  • ▸Open-source (MIT license) framework
  • ▸Commercial development of enterprise applications
  • ▸SaaS platforms using AdonisJS backends
  • ▸Integration with real-time services and queues
  • ▸Consulting and training for Node.js teams

Productivity Tips

  • ▸Use CLI generators for controllers, models, and migrations
  • ▸Leverage Lucid ORM relationships and query builders
  • ▸Implement middleware for cross-cutting concerns
  • ▸Use Edge templates efficiently for server-rendered views
  • ▸Automate testing and deployment pipelines

Basic Concepts

  • ▸Controller - handles HTTP request logic
  • ▸Model - interacts with database via Lucid ORM
  • ▸View - renders templates (optional for APIs)
  • ▸Middleware - pre/post-processing for requests
  • ▸Route - maps HTTP requests to controllers

Official Docs

  • ▸https://docs.adonisjs.com/
  • ▸AdonisJS GitHub repository
  • ▸Community tutorials and guides

Practice Other Languages

CReactPythonC++RustTypeScriptKotlinPHPJavaC#RubyMqlCqlN1qlCypher