What's the best approach to use an API and a MVC in the same server?

Hey guys, I’m new with AdonisJS and I have a project that consists of a dashboard that uses Edge views(MVC) and a mobile app that will consume an API. What is the best way to organize my application? Should I use the same controllers or separate them in different folders? Is it ok to use two authenticators(JWT and Sessions)? Thanks.

I use Adonis as a plain API with no views in a separate app like api.site.com as backend.

Then i use Quasar as frontends. I have separate sites for admin.site.com and www.site.com.

The great thing with Quasar is that it uses the same codebase to create all you need:

  • SPAs (Single Page App)
    
  • SSR (Server-side Rendered App) (+ optional PWA client takeover)
    
  • PWAs (Progressive Web App)
    
  • Mobile Apps (Android, iOS, …) through Cordova or Capacitor
    
  • Multi-platform Desktop Apps (using Electron)
    

Quasar’s motto is: write code once and simultaneously deploy it as a website, a Mobile App and/or an Electron App.

2 Likes

Separating like Peter suggested is one way (which I usually do myself too)

But other option is to have it all together. If you put them together then it’s good idea to group API routes and add some prefix to them, for example ‘api’

https://adonisjs.com/docs/4.1/routing#_route_groups

Then create separate folder in /controllers folder and call that api like controllers/api/UserController.js and put all API logic into those controllers.

To reference controllers in subfolder in route just add folder name in front of them

Route.group(() => {
  Route.get('users', 'api/UserController.index')   // GET /api/v1/users
  Route.post('users', 'api/UserController.store')  // POST /api/v1/users
}).prefix('api/v1')
2 Likes

Thanks for the answer. I will definitely consider using Quasar.

Thanks for the answer. If my application have a lot of items in admin panel, will the separate approach still be a good idea? Sometimes I think MVC would be easier in this case.

When to separate and when to not is whole another topic.

In most cases when you have other people / teams working on other sections of code, then you would want to split it up or when you need to scale one part and not another part of application etc.

There is nothing wrong with either approaches and you can always switch (even tho it might take some dev time, depending on project size).

If you are solo dev and you feel more comfortable with single application then go with that :slight_smile: Most likely there is no need to overcomplicate, especially when you are new to separating things.

1 Like