Can AdonisJ be used for REST APIS?

Sorry for the nooby question. I would have asked him anyway! I am playing with AdonisJs. I understand that this is a frame MVC

. But I want to write REST APIs

using the above structure. I couldn't find much help online.

I have two questions:

  • Does the framework support REST APIs?
  • If so, 1. what could be the best starting point?
+4


source to share


2 answers


1. I created 3 API projects with AdonisJS and find it ideal for quick setup. It has many features already included right from the start, supports database migrations and is generally well documented.

You can create routes easily with JSON responses: http://adonisjs.com/docs/3.2/response

Route.get('/', function * (request, response) {
  const users = yield User.all()
  response.json(users)
})

      

Or add them to your controller and even add token-protected route authentication quite easily (all documented):



Route.post('my_api/v1/authenticate', 'ApiController.authenticate')

Route.group('api', function () {
  Route.get('users', 'ApiController.getUsers')
}).prefix('my_api/v1').middleware('auth:api')

      

2. Take a look at the official tutorial, you can finish it in about half an hour. http://adonisjs.com/docs/3.2/overview#_simplest_example

  • Start by defining some routes and echoing simple variables with JSON and just plain views.
  • Move Test Logic to Controllers
  • Read a little more about database migration and add some simple models.
  • Don't forget Commands and Factory, as you can easily define commands for your test data. This will save you a lot of time in the long run.

Just keep in mind that you need to have a server with Node.JS installed to get the system up and running on production (I personally support it with a tool like Node Forever JS.

+7


source


To create a simple RESTful API, you can use



npm i -g @adonisjs/cli
# Create a new Adonis app
adonis new project-name --api-only

      

0


source







All Articles