🎮 Basic Tasks
📋 Task 1: Get All Users
fetch('https://api.techshare.cc/api/users')
.then(response => response.json())
.then(data => {
document.getElementById('users').textContent = JSON.stringify(data);
})
JSON.stringify(data) converts JavaScript objects to readable text.
Run Task 1
Results will appear here...
👤 Task 2: Get a Specific User
fetch('https://api.techshare.cc/api/users/693c0a4357cce1118095f635')
.then(response => response.json())
.then(data => {
document.getElementById('user').textContent = JSON.stringify(data);
})
Run Task 2
Results will appear here...
📝 Task 3: Get All Posts
fetch('https://api.techshare.cc/api/posts')
.then(response => response.json())
.then(data => {
const titles = data.map(post => post.title);
document.getElementById('posts').textContent = JSON.stringify(titles);
})
.map() extracts just the title from each post.
Run Task 3
Results will appear here...
➕ Task 4: Create a New Post
Post Title:
Post Content:
Published
const title = document.getElementById('postTitle').value;
const content = document.getElementById('postContent').value;
const published = document.getElementById('postPublished').checked;
fetch('https://api.techshare.cc/api/posts', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({
title: title,
content: content,
userId: '693c0a4357cce1118095f635',
published: published
})
})
.then(response => response.json())
.then(data => {
document.getElementById('create').textContent = JSON.stringify(data);
})
method: 'POST' creates new data.
userId is hardcoded to user 693c0a4357cce1118095f635.
Run Task 4
Results will appear here...
💡 Bonus Challenges
🔍 Challenge 1: Filter Posts by User ID
First, run Task 2 to see user details. Note the id field (not _id).
Enter User ID to filter by:
const userId = document.getElementById('userIdInput').value;
fetch('https://api.techshare.cc/api/posts')
.then(response => response.json())
.then(posts => {
const userPosts = posts.filter(post => post.userId === Number(userId));
document.getElementById('filter').textContent =
'Posts by user ID ' + userId + ': ' + JSON.stringify(userPosts);
})
Number(userId) converts text to number for comparison.
Posts use userId (like 1, 2, 3), not MongoDB _id.
Run Challenge 1
Results will appear here...