Quickstarters
How to Build a Nuxt.js Frontend and Connect it to a Backend?
35 min
in this tutorial, you’ll build a to do list application using nuxt js and connect it to a managed backend service powered by back4app this guide is tailored for you if you want to master essential crud (create, read, update, delete) operations and craft a dynamic, responsive interface using nuxt js by the end of this tutorial, your application will seamlessly interact with a backend that manages data storage, authentication, and more developing a full stack application can be challenging due to backend configuration and database management to streamline your process, we use back4app— a powerful backend as a service —allowing you to focus on developing a feature rich nuxt js frontend back4app provides a fully managed nosql database, user authentication, cloud code for custom logic, and sdks for effortless integration this enables you to build and deploy scalable applications without handling server infrastructure key takeaways by following this tutorial, you will initialize a modern nuxt js project using the nuxt cli integrate a backend service to handle your application’s data implement essential crud operations for a dynamic to do list deploy your fully functional application using containerized workflows on back4app prerequisites before you begin, ensure you have node js and npm installed on your machine confirm installations with node v and npm v basic nuxt js knowledge , including components, pages, and asynchronous data fetching a back4app account to manage your backend services sign up at back4app https //www back4app com/ if you haven’t already with these prerequisites in place, you’re ready to start building your project project setup begin by setting up your local development environment and initializing a new nuxt js project ensure you have node js (lts version) installed if not, download it from nodejs org https //nodejs org/ create your nuxt js project using the nuxt cli npx nuxi init todo app navigate into your project directory cd todo app install the necessary dependencies npm install start the development server to verify the setup npm run dev open the provided url in your browser to see your nuxt js app in action with your frontend ready, proceed to create your backend on back4app creating the todo backend back4app offers a fully managed backend service powered by parse , which includes a nosql database, authentication, cloud code, and auto generated apis follow these steps to set up your backend log in to your back4app dashboard https //www back4app com/ and click "create a new app " name your app (for example, todoapp ) and select nodejs/parse as the backend type navigate to "database" > "browser" , click "create a class" , and choose "custom" name the class task and set its permissions to allow public read and write (you can refine these settings later) in the task class, add the following fields title (string) – the title of the task description (string) – details about the task completed (boolean) – task completion status duedate (date) – the deadline for the task click "save" to create the schema integrating back4app with nuxt js you will use the parse javascript sdk to connect your nuxt js app to the back4app backend install the parse sdk npm install parse configure the sdk by creating a dedicated module for example, create plugins/parse client js with the following content import parse from 'parse/dist/parse min js'; const parse app id = 'your application id'; const parse js key = 'your javascript key'; const parse server url = 'https //parseapi back4app com/'; parse initialize(parse app id, parse js key); parse serverurl = parse server url; export default parse; then, register this plugin in your nuxt config ts (or nuxt config js ) export default { plugins \[ { src ' /plugins/parse client js', mode 'client' } ] } you can now import parse in your pages or components to interact with the backend developing the frontend your nuxt js application will consist of pages and components to add, display, update, and delete tasks leverage nuxt’s file based routing and async data fetching to create a dynamic to do list structuring your components create a components folder in your project to organize your ui components mkdir components you will create the following components taskform vue – for adding new tasks tasklist vue – to display the list of tasks taskitem vue – to represent individual tasks taskform vue this component renders a form that captures task details and submits them to back4app \<template> \<form @submit prevent="handlesubmit"> \<input type="text" placeholder="task title" v model="title" required /> \<input type="text" placeholder="description" v model="description" required /> \<button type="submit">add task\</button> \</form> \</template> \<script> import parse from 'parse/dist/parse min js'; export default { name 'taskform', data() { return { title '', description '' }; }, methods { async handlesubmit() { try { const task = parse object extend('task'); const task = new task(); task set('title', this title); task set('description', this description); task set('completed', false); await task save(); this $emit('refresh'); this title = ''; this description = ''; } catch (error) { console error('error adding task ', error); } } } }; \</script> taskitem vue this component represents an individual task and provides buttons to toggle its completion status or delete the task \<template> \<div \ class="\['task item', { completed task completed }]"> \<div> \<h3>{{ task title }}\</h3> \<p>{{ task description }}\</p> \</div> \<div> \<button @click="togglecomplete"> {{ task completed ? 'undo' 'complete' }} \</button> \<button @click="deletetask">delete\</button> \</div> \</div> \</template> \<script> import parse from 'parse/dist/parse min js'; export default { name 'taskitem', props \['task'], methods { async togglecomplete() { try { const query = new parse query('task'); const tasktoupdate = await query get(this task id); tasktoupdate set('completed', !this task completed); await tasktoupdate save(); this $emit('refresh'); } catch (error) { console error('error updating task ', error); } }, async deletetask() { try { const query = new parse query('task'); const tasktodelete = await query get(this task id); await tasktodelete destroy(); this $emit('refresh'); } catch (error) { console error('error deleting task ', error); } } } }; \</script> tasklist vue this component displays the list of tasks by iterating over an array and rendering each task using the taskitem component \<template> \<div> \<p v if="tasks length === 0">no tasks available\</p> \<taskitem v for="task in tasks" \ key="task id" \ task="task" @refresh="refreshtasks" /> \</div> \</template> \<script> import taskitem from ' /taskitem vue'; export default { name 'tasklist', props \['tasks'], methods { refreshtasks() { this $emit('refresh'); } }, components { taskitem } }; \</script> page integration in your main page (e g , pages/index vue ), manage the state of tasks and integrate your components \<template> \<div class="container"> \<h1>to do list\</h1> \<taskform @refresh="fetchtasks" /> \<tasklist \ tasks="tasks" @refresh="fetchtasks" /> \</div> \</template> \<script> import taskform from ' /components/taskform vue'; import tasklist from ' /components/tasklist vue'; import parse from 'parse/dist/parse min js'; export default { components { taskform, tasklist }, data() { return { tasks \[] }; }, methods { async fetchtasks() { try { const task = parse object extend("task"); const query = new parse query(task); const results = await query find(); this tasks = results map((task) => ({ id task id, task tojson() })); } catch (error) { console error("error fetching tasks ", error); } }, }, mounted() { this fetchtasks(); } }; \</script> styling the application create a global stylesheet (e g , assets/css/main css ) to add basic styling container { max width 600px; margin 40px auto; padding 0 20px; text align center; font family sans serif; } h1 { margin bottom 20px; } form { display flex; flex direction column; align items center; gap 10px; margin bottom 20px; } form input\[type="text"] { width 80%; max width 400px; padding 8px; box sizing border box; font size 1rem; } form button { padding 8px 16px; cursor pointer; font size 1rem; border none; background color #eaeaea; transition background color 0 2s ease; } form button\ hover { background color #ccc; } task item { display flex; flex direction column; align items center; justify content center; gap 12px; border 1px solid #ccc; border radius 6px; padding 15px; margin 10px 0; background color #fafafa; text align center; transition background color 0 2s ease; } task item completed h3, task item completed p { text decoration line through; color #888; } task item h3 { margin 0; font size 1 1rem; } task item p { margin 0; font size 1rem; } task item button { padding 6px 12px; border none; background color #eaeaea; cursor pointer; font size 0 9rem; } task item button\ hover { background color #ccc; } @media (min width 600px) { task item { flex direction row; } } include this stylesheet in your nuxt configuration by adding it to the css array in nuxt config ts export default { css \[' /assets/css/main css'] } deploying the frontend on back4app web deployment back4app web deployment offers a containerized environment to host your nuxt js application configuring nuxt js for production update your nuxt config ts if necessary to set the base path for deployment export default { router { base ' /' } } generate the production build npm run build npm run generate creating a dockerfile create a dockerfile in your project root with the following content \# stage 1 build the nuxt js app from node 18 alpine as build workdir /app copy package json package lock json / run npm install copy run npm run build run npm run generate \# stage 2 serve the app using nginx from nginx\ alpine copy from=build /app/dist /usr/share/nginx/html expose 80 cmd \["nginx", " g", "daemon off;"] testing the docker container locally build your docker image docker build t todo frontend run the container docker run p 8080 80 todo frontend open http //localhost 8080 in your browser to verify that your nuxt js app is served correctly deploying to back4app initialize a git repository, add your project files, and commit git init git add git commit m "initial commit for back4app deployment" git branch m main git remote add origin \<your github repository url> git push u origin main log in to back4app web deployment https //www back4app com/containers click "create new app" , provide your project name, and select your github repository authorize back4app and choose dockerfile deployment confirm the build settings and click "deploy" to initiate the deployment monitoring your deployment after deployment, use the back4app dashboard to view logs for debugging monitor container performance and resource usage trigger rebuilds on new commits configure custom domains if needed access your live application at https //todoapp ku5ofg8s b4a run/ https //todoapp ku5ofg8s b4a run/ https //todoapp ku5ofg8s b4a run/ testing and debugging after deployment, confirm that your nuxt js frontend properly communicates with the back4app backend api verification use your browser’s developer tools (f12 → network) to inspect api calls task operations add, complete, and delete tasks via the ui and verify updates in the back4app database browser error handling check console logs for errors and test edge cases such as empty submissions performance testing simulate slow network conditions using browser tools to assess responsiveness best practices for using back4app with nuxt js to optimize your application efficient requests use batch operations for handling multiple tasks const tasks = \[task1, task2, task3]; parse object saveall(tasks); optimized queries retrieve only the necessary fields query select('title', 'completed'); environment management store sensitive keys in environment variables nuxt public parse app id=your app id nuxt public parse js key=your js key security use acls to restrict access task setacl(new parse acl(parse user current())); backend offloading leverage cloud code for complex logic to reduce api exposure conclusion you have now built a full stack to do list application by creating a nuxt js frontend, integrating it with back4app’s backend, and deploying it using containerized workflows this tutorial guided you through every step—from local development to cloud deployment—ensuring seamless interaction between your nuxt js ui and backend services looking ahead, consider enhancements like real time updates, improved authentication, and third party integrations for further learning, visit the back4app documentation https //www back4app com/docs and explore community resources