0
0
Vueframework~20 mins

Server routes and API in Vue - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Server Routes & API Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2:00remaining
What does this Vue component display after fetching data?

Consider this Vue 3 component using the Composition API. It fetches user data from an API route and displays the user's name.

import { ref, onMounted } from 'vue';

export default {
  setup() {
    const userName = ref('');
    onMounted(async () => {
      const response = await fetch('/api/user');
      const data = await response.json();
      userName.value = data.name;
    });
    return { userName };
  }
}

Assuming the API returns { "name": "Alice" }, what will the component render inside <div>?

Vue
<template>
  <div>{{ userName }}</div>
</template>

<script setup>
import { ref, onMounted } from 'vue';

const userName = ref('');
onMounted(async () => {
  const response = await fetch('/api/user');
  const data = await response.json();
  userName.value = data.name;
});
</script>
AThe div will remain empty because fetch is asynchronous and userName is never updated.
BThe div will show 'Alice' after the fetch completes.
CThe div will show '[object Promise]' because fetch returns a promise.
DThe div will show 'undefined' because data.name is not set.
Attempts:
2 left
💡 Hint

Remember that ref is reactive and updates the template when its value changes.

📝 Syntax
intermediate
1:30remaining
Which option correctly defines a Vue 3 API route handler using server routes?

In a Vue 3 project using server routes, you want to create an API endpoint that returns JSON with a message.

Which code snippet correctly exports the handler?

Aexport default async function() { return JSON.stringify({ message: 'Hello' }) }
Bexport function handler() { return { message: 'Hello' } }
Cexport default () => { return { message: 'Hello' } }
Dexport default defineEventHandler(() => ({ message: 'Hello' }))
Attempts:
2 left
💡 Hint

Look for the official way to define event handlers in Vue server routes.

🔧 Debug
advanced
2:00remaining
Why does this Vue 3 server route handler cause a runtime error?

Examine this server route handler code:

export default defineEventHandler(async (event) => {
  const body = await useBody(event);
  return { greeting: `Hello, ${body.name}` };
});

When sending a POST request with JSON { "name": "Bob" }, the server crashes with TypeError: useBody is not a function. Why?

AThe handler must not be async to use <code>useBody</code>.
BThe <code>body</code> variable is undefined because the request has no body.
CThe <code>useBody</code> function is not imported or available in this context.
DThe event parameter is missing required properties for <code>useBody</code>.
Attempts:
2 left
💡 Hint

Check if all helper functions are imported or globally available.

state_output
advanced
2:00remaining
What is the final state of the Vue component after multiple API calls?

This Vue 3 component fetches a counter value from an API and increments it on button click:

import { ref } from 'vue';

export default {
  setup() {
    const count = ref(0);
    async function fetchCount() {
      const res = await fetch('/api/counter');
      const data = await res.json();
      count.value = data.count;
    }
    async function increment() {
      await fetch('/api/counter/increment', { method: 'POST' });
      await fetchCount();
    }
    fetchCount();
    return { count, increment };
  }
}

If the initial API returns { count: 5 } and each increment increases the count by 1 on the server, what will count.value be after calling increment() twice?

Vue
<template>
  <div>{{ count }}</div>
  <button @click="increment">Increment</button>
</template>
A7
B6
C8
D5
Attempts:
2 left
💡 Hint

Each increment call updates the server count and then fetches the new value.

🧠 Conceptual
expert
1:30remaining
Which statement best describes the role of server routes in Vue 3 projects?

Choose the most accurate description of server routes in Vue 3 applications.

AServer routes handle backend logic and API responses, allowing separation from frontend code while sharing the same project.
BServer routes are used only for client-side routing and navigation between pages.
CServer routes automatically generate static HTML pages without any backend logic.
DServer routes replace Vue components and are used to build the entire UI.
Attempts:
2 left
💡 Hint

Think about the difference between frontend and backend responsibilities.