Skip to content

Instantly share code, notes, and snippets.

@howbizarre
Last active April 28, 2022 11:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save howbizarre/ff4e16e761f595dea0745ee176dea8d1 to your computer and use it in GitHub Desktop.
Save howbizarre/ff4e16e761f595dea0745ee176dea8d1 to your computer and use it in GitHub Desktop.
Nuxt 3 async calls to /server/api/ from view in /pages/

Nuxt 3 async calls to /server/api/ from view in /pages/

You can easily add this logic in App.vue too.

/**
* add in "/server/api/" folder as "async.ts"
* or rename it as you wish
* ref: https://jsonplaceholder.typicode.com/
**/
export default defineEventHandler(async (): Promise<any> => {
return await $fetch("https://jsonplaceholder.typicode.com/posts");
});
<script setup lang="ts">
const rspns = $fetch("/api/async");
rspns.then((data) => console.log("data: ", data));
</setup>
<template>
<div>Check browser's console for logs</div>
</template>
<style>
/**
* add in "/pages/" folder as "index.vue"
**/
</style>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment