${post.user}
${post.content}
${new Date(post.timestamp).toLocaleString()}
Discover how to create a captivating social media feed with Lovable. Our guide offers simple integration tips and customization strategies to boost your online engagement.
Book a call with an Expert
Starting a new venture? Need to upgrade your web app? RapidDev builds application with your growth in mind.
Creating a New Lovable Project for the Social Media Feed
Setting Up a Data File for the Feed Posts
feedData.js
.feedData.js
:
const posts = [
{
id: 1,
user: 'Alice',
content: 'Just enjoying a sunny day!',
timestamp: '2023-10-01T10:23:00'
},
{
id: 2,
user: 'Bob',
content: 'Loving the new features on Lovable!',
timestamp: '2023-10-01T11:45:00'
},
// Add more post objects as needed
];
export default posts;
Creating a Feed Component to Render Posts
feedComponent.js
.feedComponent.js
:
import posts from './feedData.js';
function renderPost(post) {
return `
${post.user}
${post.content}
${new Date(post.timestamp).toLocaleString()}
`;
}
function renderFeed() {
const feedContainer = document.getElementById('feed');
// Clear existing feed if any
feedContainer.innerHTML = '';
// Loop through posts and insert them into the feed
posts.forEach(post => {
feedContainer.innerHTML += renderPost(post);
});
}
export { renderFeed };
Integrating the Feed Component into Your Main HTML File
index.html
).index.html
, add an HTML container where the feed will be displayed. Also include the script tags to load your components. Insert the following code into your index.html
file in the appropriate section (usually the <body>
part):
Social Media Feed
Installing Dependencies Directly in Code
<head>
of your index.html
:
Testing and Adjusting Your Social Media Feed
feedData.js
.feedData.js
file and refresh the preview.<style>
section within index.html
or by creating a separate CSS file and linking to it.
const express = require('express');
const mongoose = require('mongoose');
const app = express();
mongoose.connect('mongodb://localhost/lovable', { useNewUrlParser: true, useUnifiedTopology: true });
const PostSchema = new mongoose.Schema({
userId: String,
content: String,
createdAt: { type: Date, default: Date.now }
});
const LikeSchema = new mongoose.Schema({
postId: mongoose.Schema.Types.ObjectId,
userId: String,
likedAt: { type: Date, default: Date.now }
});
const Post = mongoose.model('Post', PostSchema);
const Like = mongoose.model('Like', LikeSchema);
app.get('/api/feed', async (req, res) => {
const page = parseInt(req.query.page) || 1;
const limit = parseInt(req.query.limit) || 10;
const skip = (page - 1) \* limit;
try {
const feed = await Post.aggregate([
{ $sort: { createdAt: -1 } },
{ $skip: skip },
{ $limit: limit },
{
$lookup: {
from: 'likes',
localField: '\_id',
foreignField: 'postId',
as: 'likes'
}
},
{ $addFields: { likesCount: { $size: '$likes' } } },
{ $project: { likes: 0 } }
]);
res.json({ page, limit, data: feed });
} catch (err) {
res.status(500).json({ error: err.message });
}
});
const PORT = 3000;
app.listen(PORT, () => {
console.log(`Server running on port ${PORT}`);
});
const express = require('express');
const axios = require('axios');
const app = express();
const posts = [
{ id: 1, userId: 'user1', content: 'Just joined Lovable!', createdAt: new Date() },
{ id: 2, userId: 'user2', content: 'Loving the new features.', createdAt: new Date() },
{ id: 3, userId: 'user3', content: 'This feed is lit!', createdAt: new Date() }
];
app.get('/api/feed/enhanced', async (req, res) => {
try {
const postIds = posts.map(post => post.id);
const externalApiUrl = 'https://api.externalservice.com/lovable-stats';
const response = await axios.post(externalApiUrl, { ids: postIds });
const analytics = response.data;
const enhancedFeed = posts.map(post => ({
...post,
likes: analytics[post.id] ? analytics[post.id].likes : 0,
shares: analytics[post.id] ? analytics[post.id].shares : 0
}));
res.json({ feed: enhancedFeed });
} catch (error) {
res.status(500).json({ error: error.message });
}
});
const PORT = process.env.PORT || 3001;
app.listen(PORT, () => {
console.log(`Server running on port ${PORT}`);
});
const express = require('express');
const mongoose = require('mongoose');
const axios = require('axios');
const app = express();
mongoose.connect('mongodb://localhost/lovable', { useNewUrlParser: true, useUnifiedTopology: true });
const PostSchema = new mongoose.Schema({
userId: String,
content: String,
createdAt: { type: Date, default: Date.now }
});
const FollowSchema = new mongoose.Schema({
follower: String,
following: String
});
const Post = mongoose.model('Post', PostSchema);
const Follow = mongoose.model('Follow', FollowSchema);
app.get('/api/homefeed/:userId', async (req, res) => {
const { userId } = req.params;
const page = parseInt(req.query.page) || 1;
const limit = parseInt(req.query.limit) || 15;
const skip = (page - 1) \* limit;
try {
const followingDocs = await Follow.find({ follower: userId }).select('following');
const followingIds = followingDocs.map(doc => doc.following);
followingIds.push(userId);
const userPostsPromise = Post.find({ userId: { $in: followingIds } })
.sort({ createdAt: -1 })
.skip(skip)
.limit(limit)
.lean();
const trendingPostsPromise = axios.get('https://api.lovable.com/trending')
.then(response => response.data.posts || []);
const [userPosts, trendingPosts] = await Promise.all([userPostsPromise, trendingPostsPromise]);
const combinedPosts = [...userPosts, ...trendingPosts];
combinedPosts.sort((a, b) => new Date(b.createdAt) - new Date(a.createdAt));
const feed = combinedPosts.slice(0, limit);
res.json({ page, limit, data: feed });
} catch (err) {
res.status(500).json({ error: err.message });
}
});
const PORT = process.env.PORT || 4000;
app.listen(PORT, () => {
console.log(`Server running on port ${PORT}`);
});
Book a call with an Expert
Starting a new venture? Need to upgrade your web app? RapidDev builds application with your growth in mind.
Prerequisites
Planning Your Social Media Feed Structure
When it comes to serving you, we sweat the little things. That’s why our work makes a big impact.