// server.
js
const express = require('express');
const mongoose = require('mongoose');
const multer = require('multer');
const path = require('path');
const app = express();
// Connect to MongoDB
[Link]('mongodb://localhost/tiktok-clone', { useNewUrlParser: true,
useUnifiedTopology: true })
.then(() => [Link]('MongoDB connected'))
.catch(err => [Link]('MongoDB connection error:', err));
// Define video schema
const videoSchema = new [Link]({
title: String,
description: String,
video: String,
thumbnail: String,
});
// Create Video model
const Video = [Link]('Video', videoSchema);
// Set up multer for file uploads
const storage = [Link]({
destination: (req, file, cb) => {
cb(null, './uploads');
},
filename: (req, file, cb) => {
cb(null, [Link]);
},
});
const upload = multer({ storage: storage });
// Middleware to parse JSON bodies
[Link]([Link]());
// Get all videos
[Link]('/videos', async (req, res) => {
try {
const videos = await [Link]();
[Link](videos);
} catch (err) {
[Link](500).json({ message: [Link] });
}
});
// Upload a new video
[Link]('/videos', [Link]('video'), async (req, res) => {
const video = new Video({
title: [Link],
description: [Link],
video: [Link],
thumbnail: [Link] + '.jpg', // Assuming thumbnail is generated
separately
});
try {
await [Link]();
[Link](201).json(video);
} catch (err) {
[Link](400).json({ message: [Link] });
}
});
// Serve static files from the uploads directory
[Link]('/uploads', [Link]([Link](__dirname, 'uploads')));
// Start the server
[Link](3000, () => {
[Link]('Server listening on port 3000');
});