Nuxt Server
up:: Nuxt ⚐
API
You can create a folder structure like so:
server/
api/
getBooks.js
In getBooks.js
You can make an api call:
export default defineEventHandler(async (event) => {
const { data } await $fetch('https://api.books.site');
return data;
})
In the script setup of any page:
const { data } = await useFetch('api/getBooks.js');
The benefit of doing that instead of in a Nuxt Pages script setup
function is that private API keys remain hidden.
This also work with Nuxt Dynamic routes to pass in query parameters in the url.