0% found this document useful (0 votes)
4 views2 pages

TikTok Clone Video Upload API

This document is a Node.js server application using Express and Mongoose to create a video upload service. It connects to a MongoDB database, defines a video schema, and provides endpoints to get all videos and upload new videos with file handling using Multer. The server listens on port 3000 and serves static files from an uploads directory.

Uploaded by

hatbewafa786
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views2 pages

TikTok Clone Video Upload API

This document is a Node.js server application using Express and Mongoose to create a video upload service. It connects to a MongoDB database, defines a video schema, and provides endpoints to get all videos and upload new videos with file handling using Multer. The server listens on port 3000 and serves static files from an uploads directory.

Uploaded by

hatbewafa786
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd

// 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');
});

You might also like