Cannot import style in view on a project created with --api-only

I have a REST API built with AdonisJS and created with the flag --api-only. I’ve implemented all the logic for password retrieving using the Token model available on the project. Now I need to render a static page with a form for the user to enter his new password.
Here are the steps I took:

  1. I put ‘@adonisjs/framework/providers/ViewProvider’ inside my app providers, on start/app.js
  2. I created a new edge template on resources/views/edge.template
  3. I’ve created a Route with Route.on(’/resetpassword’).render(‘resetpassword’); At this point I’m able to render this page with no problem.
  4. I’ve created a css file on public/style.css
  5. I’ve added {{ style(‘style’) }} on my template.edge file.

Problem:: When the page loads no css is loaded. When I look at the inspector menu I see a bad request error: GET Request to http://localhost:3333/style.css returns 404 (NOT FOUND)

What am I missing?

2 Likes

Hey @brunobmello25! :wave:

This is the default behavior of the API Only blueprint.

If you want to use the public folder you need to enable the StaticAssetMiddleware in your kernel.js file (Here).

This middleware is removed by default because it slows down your application.

In production, you should prefer using a CDN or the web server directly (Nginx, Apache, etc.) to handle your static assets.

3 Likes

I see, thank you!
And how should I import my assets if they are provided from a CDN or another server?

Exactly like you would import them otherwise in HTML / Edge, with <script src="https://place.com/script.js"></script>