Examining Full-Stack Needs
I'm now diving deeper into the "full-stack" aspect. It seems crucial to delineate the backend requirements. This includes the database, API structure, and deployment strategies. Considering the front-end specifics, I'm leaning toward a RESTful API.
Assessing Project Architecture
I'm now contemplating project architecture. The initial focus is on a Monorepo strategy with a Node/Express backend. I'm leaning toward defining the folder structure, detailing initialization commands, and outlining essential file contents. I'm also thinking of a concurrently running script. The goal is a highly flexible yet ready-to-use foundation.
Outlining Boilerplate Steps
I'm now deep-diving into the step-by-step instructions. I'm focusing on clarity. I'll outline project creation, then a Node/Express backend, and a Vue 3/Vite/Pinia frontend. I'm working to provide simple "Hello World" examples for each layer, connecting everything with a proxy and concurrently. I'm going to note the essential dependencies for installation too.
Formulating Boilerplate Structure
I'm now consolidating the steps. I've broken down the project initialization, starting with folder creation, moving through backend setup (Node/Express), then frontend (Vue 3, Vite, Pinia). Crucially, I'm defining the Vite proxy configuration. I'm focusing on the "Hello World" examples, and then integrating the setup with concurrently and noting all dependencies. I'm focusing on simplicity with a root package.json.
Developing Full-Stack Foundation
I'm now constructing the output, having refined the plan. I'm focusing on the introduction, clearly stating the stack. I'm detailing the folder structure and setup commands. I'm also streamlining the "Hello World" example in the Vue component, Pinia store, backend API endpoint, and Vite proxy configuration. I'm adding instructions on how to run the app.
This boilerplate assumes a Node.js (Express) backend, as it is the most common pairing with Vue. We will create a "monorepo-lite" structure where the root folder manages executing both the Client and the Server simultaneously using concurrently.
Here is the complete guide to setting up a Vue 3 + Vite + Pinia + Express full-stack application.
1. Project Structure
We will organize the project like this:
my-fullstack-app/
├── client/ # Vue 3 + Vite + Pinia
├── server/ # Node.js + Express
└── package.json # Root scripts to run both
2. Root Setup (Orchestration)
First, create the main folder and initialize the root package.
mkdir my-fullstack-app
cd my-fullstack-app
npm init -y
npm install concurrently --save-dev
Update the root package.json scripts to run both sides at once:
{
"name": "my-fullstack-app",
"version": "1.0.0",
"scripts": {
"client": "cd client && npm run dev",
"server": "cd server && npm run dev",
"dev": "concurrently \"npm run server\" \"npm run client\""
},
"devDependencies": {
"concurrently": "^8.2.0"
}
}
3. Backend Setup (Node/Express)
Set up the server inside the server folder.
mkdir server
cd server
npm init -y
npm install express cors dotenv
npm install nodemon --save-dev
Create a server/package.json script update (for hot-reloading Node):
"scripts": {
"dev": "nodemon index.js",
"start": "node index.js"
}
Create server/index.js:
const express = require('express');
const cors = require('cors');
const app = express();
const PORT = process.env.PORT || 3000;
// Middleware
app.use(cors());
app.use(express.json());
// Routes
app.get('/api/status', (req, res) => {
res.json({ message: "Backend is running!", timestamp: new Date() });
});
app.listen(PORT, () => {
console.log(`Server running on http://localhost:${PORT}`);
});
Go back to root: cd ..
4. Frontend Setup (Vue 3 + Vite + Pinia)
Set up the client inside the client folder.
npm create vite@latest client -- --template vue
cd client
npm install
npm install pinia axios
A. Configure Vite Proxy
To prevent CORS errors during development and make API calls clean (e.g., using /api instead of http://localhost:3000/api), modify client/vite.config.js:
import { defineConfig } from 'vite'
import vue from '@vitejs/plugin-vue'
export default defineConfig({
plugins: [vue()],
server: {
proxy: {
'/api': {
target: 'http://localhost:3000', // Points to your backend
changeOrigin: true,
}
}
}
})
B. Setup Pinia
Modify client/src/main.js to register Pinia:
import { createApp } from 'vue'
import { createPinia } from 'pinia'
import './style.css'
import App from './App.vue'
const pinia = createPinia()
const app = createApp(App)
app.use(pinia)
app.mount('#app')
C. Create a Store
Create client/src/stores/appStore.js:
import { defineStore } from 'pinia'
import axios from 'axios'
export const useAppStore = defineStore('app', {
state: () => ({
message: 'Waiting for backend...',
loading: false
}),
actions: {
async fetchStatus() {
this.loading = true;
try {
// Using relative path thanks to Vite Proxy
const response = await axios.get('/api/status');
this.message = response.data.message;
} catch (error) {
this.message = 'Error connecting to server';
console.error(error);
} finally {
this.loading = false;
}
}
}
})
D. Connect View (App.vue)
Modify client/src/App.vue:
<script setup>
import { onMounted } from 'vue';
import { useAppStore } from './stores/appStore';
const store = useAppStore();
onMounted(() => {
store.fetchStatus();
});
</script>
<template>
<div class="container">
<h1>Vue 3 + Vite + Pinia + Express</h1>
<div class="card">
<p><strong>Backend Status:</strong></p>
<p v-if="store.loading">Loading...</p>
<p v-else class="status">{{ store.message }}</p>
</div>
<button @click="store.fetchStatus">Refresh Status</button>
</div>
</template>
<style scoped>
.container { text-align: center; font-family: sans-serif; margin-top: 50px; }
.card { border: 1px solid #ccc; padding: 20px; margin: 20px auto; max-width: 400px; border-radius: 8px; }
.status { color: #42b883; font-weight: bold; font-size: 1.2rem; }
button { padding: 10px 20px; cursor: pointer; }
</style>
5. Run the Application
Go back to the root directory of your project (my-fullstack-app) and run:
npm run dev
What happens next?
concurrently starts.
- Server starts on port 3000 (watched by Nodemon).
- Client starts on port 5173 (watched by Vite).
- Open
http://localhost:5173.
- The page loads, Vue mounts, Pinia calls the API action.
- Vite proxies the request to port 3000.
- You see "Backend is running!" displayed on the screen.
Optional: Recommended Additions
For a production-ready app, you usually want to add:
- Vue Router:
npm install vue-router@4 (in client).
- Backend Structure: Use folders like
controllers/routes in the server folder rather than putting everything in index.js.
- Database: Install
mongoose (MongoDB) or sequelize/pg (SQL) in the server folder.