Api routes
Api routes can be created the same way as page routes. To register an api route, just create
a file inside your Api Directory
.
All api routes will be prefixed with api
. If you create a users.js
inside the root of your Api directory
the final url will be /api/users
.
Get request
Example of a basic route which will react on a get request.
export default async ({ request, response }) => {
return response.json({ result: "success" });
}
Post request
Example of a basic route which will react on a post request.
const post = async ({ request, response }) => {
return response.json({ result: "success" });
};
export { post };
Mixed requests
const post = async ({ request, response }) => {
return response.json({ result: "get success" });
};
const get = async ({ request, response }) => {
return response.json({ result: "post success" });
};
export { post, get };
Using dependency injection
Luna passes a container
to every api route which can be used
to retrieve defined services.
import AuthService from '../services/auth-service.js';
export default async ({ request, response, container }) => {
const authService = container.get(AuthService);
return response.json(authService.getCurrentUser());
};
Setting a different api domain
To set a different api domain, you can change the context
and domain
properties inside the api
section of your luna.config.js.
{
api: {
domain: "<url-to-your-api-host>"
context: "<optional-context-path>"
}
}