Here’s a minimal end-to-end example that shows how to…
Spin up a small Express+SQLite backend to record clicks
Create a Vue.js front-end that intercepts link-clicks, sends the URL to your API, then navigates
1) Backend (Node.js + Express + sqlite3)
a) package.json
json Copy code
{
"name": "click-logger",
"version": "1.0.0",
"main": "server.js",
"scripts": {
"start": "node server.js"
},
"dependencies": {
"body-parser": "^1.20.0",
"cors": "^2.8.5",
"express": "^4.18.1",
"sqlite3": "^5.0.11"
}
}
b) server.js
js Copy code
const express = require('express');
const bodyParser = require('body-parser');
const sqlite3 = require('sqlite3');
const cors = require('cors');
const path = require('path');
const app = express();
const port = 3000;
// Enable CORS (from your Vue dev server)
app.use(cors());
app.use(bodyParser.json());
// Open (or create) the SQLite database file
const db = new sqlite3.Database(path.resolve(__dirname, 'clicks.db'));
// Create table if it doesn't exist
db.run(`
CREATE TABLE IF NOT EXISTS link_clicks (
url TEXT PRIMARY KEY,
count INTEGER DEFAULT 0
)
`);
// API endpoint to log clicks
app.post('/api/log', (req, res) => {
const { url } = req.body;
if (!url) {
return res.status(400).json({ error: 'Missing url' });
}
// Upsert: increment count if exists, otherwise insert with count=1
const sql = `
INSERT INTO link_clicks(url, count)
VALUES(?, 1)
ON CONFLICT(url) DO UPDATE SET count = count + 1
`;
db.run(sql, [url], function (err) {
if (err) {
console.error(err);
return res.status(500).json({ error: 'DB error' });
}
// Return the new count
db.get(
`SELECT count FROM link_clicks WHERE url = ?`,
[url],
(err2, row) => {
if (err2) {
console.error(err2);
return res.status(500).json({ error: 'DB error' });
}
res.json({ url, count: row.count });
}
);
});
});
// (Optional) API to list all links + counts
app.get('/api/stats', (req, res) => {
db.all(`SELECT url, count FROM link_clicks`, [], (e, rows) => {
if (e) return res.status(500).json({ error: e.message });
res.json(rows);
});
});
app.listen(port, () => {
console.log(`Click-logger API running at http://localhost:${port}`);
});
Install & run
bash Copy code
npm install
npm start
2) Front-end (Vue.js + Axios)
You can use the Vue CLI or Vite. Here’s a single‐file component approach:
a) Install Axios
bash Copy code
npm install axios
b) App.vue
html Copy code
<template>
<div id="app">
<h1>Tracked Links</h1>
<ul>
<li v-for="link in links" :key="link.url">
<!-- We prevent the native navigation, log, then navigate manually -->
<a
:href="link.url"
@click.prevent="trackAndGo(link.url)"
>{{ link.name }}</a>
</li>
</ul>
<h2>Click Stats</h2>
<button @click="fetchStats()">Reload Stats</button>
<ul>
<li v-for="stat in stats" :key="stat.url">
{{ stat.url }} → {{ stat.count }} clicks
</li>
</ul>
</div>
</template>
<script>
import axios from 'axios';
export default {
name: 'App',
data() {
return {
// example links
links: [
{ name: 'Vue.js', url: 'https://vuejs.org/' },
{ name: 'Google', url: 'https://google.com/' },
],
stats: []
};
},
mounted() {
this.fetchStats();
},
methods: {
async trackAndGo(url) {
try {
// 1) Tell your backend
const res = await axios.post('http://localhost:3000/api/log', { url });
console.log(`Logged click for ${res.data.url}, new count = ${res.data.count}`);
// 2) Navigate
window.location.href = url;
} catch (err) {
console.error('Logging failed', err);
// Still navigate on error
window.location.href = url;
}
},
async fetchStats() {
const res = await axios.get('http://localhost:3000/api/stats');
this.stats = res.data;
}
}
};
</script>
<style>
#app { font-family: Avenir, Helvetica, Arial, sans-serif; padding: 2em; }
</style>
How it works
Frontend
– Every <a> has an @click.prevent handler
– We call POST /api/log { url }
– On success (or failure) we do window.location.href = url
Backend
– Receives the URL,
– Does an “upsert” on SQLite (INSERT … ON CONFLICT … DO UPDATE)
– Returns the new click‐count
Stats endpoint
– Optionally you can fetch /api/stats to see how many times each link was clicked
Feel free to adapt this skeleton (e.g. use a proper Vue router, refine error‐handling, secure CORS, or deploy). Let me know if you need more customization! 😊