Cleaning Up Routes with Controllers

In this lesson, we'll learn what controllers are and how they can be used to drastically simplify our route definitions by allowing us to move our route handlers off the route definition and into the controller.

Published
Jan 31
Duration
4m 52s

Developer, dog lover, and burrito eater. Currently teaching AdonisJS, a fully featured NodeJS framework, and running Adocasts where I post new lessons weekly. Professionally, I work with JavaScript, .Net C#, and SQL Server.

Adocasts

Burlington, KY

Join The Discussion! (4 Comments)

Please sign in or sign up for free to join in on the dicussion.

  1. Commented 19 days ago

    Hi,

    When I save routes.ts file, imports remain identical. Do we have to do anything to make it work?

    1

    Please sign in or sign up for free to reply

    1. Commented 19 days ago

      Hi Jean!

      The standard import for controllers will work just fine, for example

      import MoviesController from '#controllers/movies_controller'
      Copied!

      However, mine changed to lazy style imports on save within the video due to the ESLint I set up and configured code actions for within VS Code in lesson 1.4 of this series.

      Below is an example of the ESLint rule error that's auto-fixed by my code action configuration when I save my routes.ts file.

      ESLint is completely optional! It essentially is a style guide for your code, and anything not matching the style guide will display an ESLint error.

      If you'd like to use it, you can install the ESLint extension within VS Code. Then, you can have it auto-fix ESLint errors by adding the below within your VS Code settings.json

      "editor.codeActionsOnSave": {
        "source.fixAll.eslint": "explicit"
      } 
      Copied!
      1

      Please sign in or sign up for free to reply

      1. Commented 19 days ago

        For mystical reasons, I missed that part. Sorry :D

        By the way, the new version of Adocasts is cool.

        1

        Please sign in or sign up for free to reply

        1. Commented 19 days ago

          No worries at all, Jean!! :D

          Thank you, I really appreciate that!!

          0

          Please sign in or sign up for free to reply

Playing Next Lesson In
seconds