Spaces:
Running
Running
می خوام توضیحاتی که دادم واقعی باشه python-learning-platform/
Browse files├── server/
│ ├── server.js
│ ├── models/
│ ├── routes/
│ └── utils/
├── client/
│ ├── index.html
│ ├── src/
│ │ ├── main.jsx
│ │ ├── App.jsx
│ │ ├── pages/
│ │ ├── context/
│ │ └── i18n/
└── .env
- .env +11 -0
- client/src/App.jsx +31 -0
- client/src/context/AuthContext.jsx +75 -0
- client/src/main.jsx +19 -0
- client/src/pages/Home.jsx +25 -0
- server/models/Course.js +32 -0
- server/models/User.js +33 -0
- server/routes/auth.js +56 -0
- server/routes/courses.js +60 -0
- server/server.js +39 -0
.env
ADDED
|
@@ -0,0 +1,11 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
```env
|
| 2 |
+
NODE_ENV=development
|
| 3 |
+
PORT=5000
|
| 4 |
+
MONGODB_URI=mongodb://localhost:27017/python-learning
|
| 5 |
+
JWT_SECRET=your_jwt_secret_key_here
|
| 6 |
+
JWT_EXPIRE=30d
|
| 7 |
+
COOKIE_EXPIRE=30
|
| 8 |
+
CLOUDINARY_CLOUD_NAME=your_cloudinary_name
|
| 9 |
+
CLOUDINARY_API_KEY=your_cloudinary_key
|
| 10 |
+
CLOUDINARY_API_SECRET=your_cloudinary_secret
|
| 11 |
+
```
|
client/src/App.jsx
ADDED
|
@@ -0,0 +1,31 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import { Routes, Route } from 'react-router-dom';
|
| 2 |
+
import Home from './pages/Home';
|
| 3 |
+
import Courses from './pages/Courses';
|
| 4 |
+
import CourseDetail from './pages/CourseDetail';
|
| 5 |
+
import Login from './pages/Login';
|
| 6 |
+
import Register from './pages/Register';
|
| 7 |
+
import Dashboard from './pages/Dashboard';
|
| 8 |
+
import Navbar from './components/Navbar';
|
| 9 |
+
import Footer from './components/Footer';
|
| 10 |
+
import PrivateRoute from './components/PrivateRoute';
|
| 11 |
+
|
| 12 |
+
function App() {
|
| 13 |
+
return (
|
| 14 |
+
<div className="flex flex-col min-h-screen">
|
| 15 |
+
<Navbar />
|
| 16 |
+
<main className="flex-grow">
|
| 17 |
+
<Routes>
|
| 18 |
+
<Route path="/" element={<Home />} />
|
| 19 |
+
<Route path="/courses" element={<Courses />} />
|
| 20 |
+
<Route path="/courses/:id" element={<CourseDetail />} />
|
| 21 |
+
<Route path="/login" element={<Login />} />
|
| 22 |
+
<Route path="/register" element={<Register />} />
|
| 23 |
+
<Route path="/dashboard" element={<PrivateRoute><Dashboard /></PrivateRoute>} />
|
| 24 |
+
</Routes>
|
| 25 |
+
</main>
|
| 26 |
+
<Footer />
|
| 27 |
+
</div>
|
| 28 |
+
);
|
| 29 |
+
}
|
| 30 |
+
|
| 31 |
+
export default App;
|
client/src/context/AuthContext.jsx
ADDED
|
@@ -0,0 +1,75 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import { createContext, useContext, useState, useEffect } from 'react';
|
| 2 |
+
import axios from 'axios';
|
| 3 |
+
import { useNavigate } from 'react-router-dom';
|
| 4 |
+
|
| 5 |
+
const AuthContext = createContext();
|
| 6 |
+
|
| 7 |
+
export function AuthProvider({ children }) {
|
| 8 |
+
const [user, setUser] = useState(null);
|
| 9 |
+
const [token, setToken] = useState(localStorage.getItem('token'));
|
| 10 |
+
const [loading, setLoading] = useState(true);
|
| 11 |
+
const navigate = useNavigate();
|
| 12 |
+
|
| 13 |
+
// Check for token and load user
|
| 14 |
+
useEffect(() => {
|
| 15 |
+
if (token) {
|
| 16 |
+
axios.defaults.headers.common['Authorization'] = `Bearer ${token}`;
|
| 17 |
+
getUser();
|
| 18 |
+
} else {
|
| 19 |
+
setLoading(false);
|
| 20 |
+
}
|
| 21 |
+
}, [token]);
|
| 22 |
+
|
| 23 |
+
const getUser = async () => {
|
| 24 |
+
try {
|
| 25 |
+
const res = await axios.get('/api/auth/me');
|
| 26 |
+
setUser(res.data);
|
| 27 |
+
} catch (err) {
|
| 28 |
+
logout();
|
| 29 |
+
} finally {
|
| 30 |
+
setLoading(false);
|
| 31 |
+
}
|
| 32 |
+
};
|
| 33 |
+
|
| 34 |
+
const register = async (formData) => {
|
| 35 |
+
try {
|
| 36 |
+
const res = await axios.post('/api/auth/register', formData);
|
| 37 |
+
setToken(res.data.token);
|
| 38 |
+
localStorage.setItem('token', res.data.token);
|
| 39 |
+
setUser(res.data.user);
|
| 40 |
+
navigate('/dashboard');
|
| 41 |
+
} catch (err) {
|
| 42 |
+
throw err.response.data;
|
| 43 |
+
}
|
| 44 |
+
};
|
| 45 |
+
|
| 46 |
+
const login = async (formData) => {
|
| 47 |
+
try {
|
| 48 |
+
const res = await axios.post('/api/auth/login', formData);
|
| 49 |
+
setToken(res.data.token);
|
| 50 |
+
localStorage.setItem('token', res.data.token);
|
| 51 |
+
setUser(res.data.user);
|
| 52 |
+
navigate('/dashboard');
|
| 53 |
+
} catch (err) {
|
| 54 |
+
throw err.response.data;
|
| 55 |
+
}
|
| 56 |
+
};
|
| 57 |
+
|
| 58 |
+
const logout = () => {
|
| 59 |
+
localStorage.removeItem('token');
|
| 60 |
+
delete axios.defaults.headers.common['Authorization'];
|
| 61 |
+
setToken(null);
|
| 62 |
+
setUser(null);
|
| 63 |
+
navigate('/');
|
| 64 |
+
};
|
| 65 |
+
|
| 66 |
+
return (
|
| 67 |
+
<AuthContext.Provider value={{ user, token, loading, register, login, logout }}>
|
| 68 |
+
{children}
|
| 69 |
+
</AuthContext.Provider>
|
| 70 |
+
);
|
| 71 |
+
}
|
| 72 |
+
|
| 73 |
+
export function useAuth() {
|
| 74 |
+
return useContext(AuthContext);
|
| 75 |
+
}
|
client/src/main.jsx
ADDED
|
@@ -0,0 +1,19 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import React from 'react';
|
| 2 |
+
import ReactDOM from 'react-dom/client';
|
| 3 |
+
import App from './App';
|
| 4 |
+
import './index.css';
|
| 5 |
+
import { BrowserRouter } from 'react-router-dom';
|
| 6 |
+
import { AuthProvider } from './context/AuthContext';
|
| 7 |
+
import { CourseProvider } from './context/CourseContext';
|
| 8 |
+
|
| 9 |
+
ReactDOM.createRoot(document.getElementById('root')).render(
|
| 10 |
+
<React.StrictMode>
|
| 11 |
+
<BrowserRouter>
|
| 12 |
+
<AuthProvider>
|
| 13 |
+
<CourseProvider>
|
| 14 |
+
<App />
|
| 15 |
+
</CourseProvider>
|
| 16 |
+
</AuthProvider>
|
| 17 |
+
</BrowserRouter>
|
| 18 |
+
</React.StrictMode>
|
| 19 |
+
);
|
client/src/pages/Home.jsx
ADDED
|
@@ -0,0 +1,25 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import { useEffect } from 'react';
|
| 2 |
+
import Hero from '../components/Hero';
|
| 3 |
+
import Features from '../components/Features';
|
| 4 |
+
import PopularCourses from '../components/PopularCourses';
|
| 5 |
+
import Testimonials from '../components/Testimonials';
|
| 6 |
+
import CTA from '../components/CTA';
|
| 7 |
+
import { useCourse } from '../context/CourseContext';
|
| 8 |
+
|
| 9 |
+
export default function Home() {
|
| 10 |
+
const { featuredCourses, getFeaturedCourses } = useCourse();
|
| 11 |
+
|
| 12 |
+
useEffect(() => {
|
| 13 |
+
getFeaturedCourses();
|
| 14 |
+
}, []);
|
| 15 |
+
|
| 16 |
+
return (
|
| 17 |
+
<>
|
| 18 |
+
<Hero />
|
| 19 |
+
<Features />
|
| 20 |
+
<PopularCourses courses={featuredCourses} />
|
| 21 |
+
<Testimonials />
|
| 22 |
+
<CTA />
|
| 23 |
+
</>
|
| 24 |
+
);
|
| 25 |
+
}
|
server/models/Course.js
ADDED
|
@@ -0,0 +1,32 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
const mongoose = require('mongoose');
|
| 2 |
+
|
| 3 |
+
const lessonSchema = new mongoose.Schema({
|
| 4 |
+
title: { type: String, required: true },
|
| 5 |
+
content: { type: String, required: true },
|
| 6 |
+
videoUrl: String,
|
| 7 |
+
duration: Number, // in minutes
|
| 8 |
+
resources: [{
|
| 9 |
+
title: String,
|
| 10 |
+
url: String
|
| 11 |
+
}],
|
| 12 |
+
quiz: { type: mongoose.Schema.Types.ObjectId, ref: 'Quiz' }
|
| 13 |
+
}, { timestamps: true });
|
| 14 |
+
|
| 15 |
+
const courseSchema = new mongoose.Schema({
|
| 16 |
+
title: { type: String, required: true },
|
| 17 |
+
description: { type: String, required: true },
|
| 18 |
+
instructor: { type: mongoose.Schema.Types.ObjectId, ref: 'User', required: true },
|
| 19 |
+
category: { type: String, required: true },
|
| 20 |
+
level: { type: String, enum: ['beginner', 'intermediate', 'advanced'], required: true },
|
| 21 |
+
price: { type: Number, default: 0 },
|
| 22 |
+
thumbnail: { type: String, required: true },
|
| 23 |
+
lessons: [lessonSchema],
|
| 24 |
+
studentsEnrolled: { type: Number, default: 0 },
|
| 25 |
+
rating: { type: Number, default: 0 },
|
| 26 |
+
requirements: [String],
|
| 27 |
+
whatYouWillLearn: [String],
|
| 28 |
+
isFeatured: { type: Boolean, default: false },
|
| 29 |
+
createdAt: { type: Date, default: Date.now }
|
| 30 |
+
});
|
| 31 |
+
|
| 32 |
+
module.exports = mongoose.model('Course', courseSchema);
|
server/models/User.js
ADDED
|
@@ -0,0 +1,33 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
const mongoose = require('mongoose');
|
| 2 |
+
const bcrypt = require('bcryptjs');
|
| 3 |
+
|
| 4 |
+
const userSchema = new mongoose.Schema({
|
| 5 |
+
username: { type: String, required: true, unique: true },
|
| 6 |
+
email: { type: String, required: true, unique: true },
|
| 7 |
+
password: { type: String, required: true },
|
| 8 |
+
role: { type: String, enum: ['student', 'instructor', 'admin'], default: 'student' },
|
| 9 |
+
enrolledCourses: [{ type: mongoose.Schema.Types.ObjectId, ref: 'Course' }],
|
| 10 |
+
completedLessons: [{
|
| 11 |
+
course: { type: mongoose.Schema.Types.ObjectId, ref: 'Course' },
|
| 12 |
+
lesson: { type: mongoose.Schema.Types.ObjectId, ref: 'Lesson' }
|
| 13 |
+
}],
|
| 14 |
+
profile: {
|
| 15 |
+
fullName: String,
|
| 16 |
+
bio: String,
|
| 17 |
+
avatar: String,
|
| 18 |
+
skills: [String]
|
| 19 |
+
},
|
| 20 |
+
createdAt: { type: Date, default: Date.now }
|
| 21 |
+
});
|
| 22 |
+
|
| 23 |
+
userSchema.pre('save', async function(next) {
|
| 24 |
+
if (!this.isModified('password')) return next();
|
| 25 |
+
this.password = await bcrypt.hash(this.password, 12);
|
| 26 |
+
next();
|
| 27 |
+
});
|
| 28 |
+
|
| 29 |
+
userSchema.methods.comparePassword = async function(candidatePassword) {
|
| 30 |
+
return await bcrypt.compare(candidatePassword, this.password);
|
| 31 |
+
};
|
| 32 |
+
|
| 33 |
+
module.exports = mongoose.model('User', userSchema);
|
server/routes/auth.js
ADDED
|
@@ -0,0 +1,56 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
const express = require('express');
|
| 2 |
+
const jwt = require('jsonwebtoken');
|
| 3 |
+
const bcrypt = require('bcryptjs');
|
| 4 |
+
const User = require('../models/User');
|
| 5 |
+
const router = express.Router();
|
| 6 |
+
|
| 7 |
+
// Register
|
| 8 |
+
router.post('/register', async (req, res) => {
|
| 9 |
+
try {
|
| 10 |
+
const { username, email, password } = req.body;
|
| 11 |
+
|
| 12 |
+
let user = await User.findOne({ email });
|
| 13 |
+
if (user) return res.status(400).json({ message: 'User already exists' });
|
| 14 |
+
|
| 15 |
+
user = new User({ username, email, password });
|
| 16 |
+
await user.save();
|
| 17 |
+
|
| 18 |
+
const token = jwt.sign({ id: user._id }, process.env.JWT_SECRET, {
|
| 19 |
+
expiresIn: '30d'
|
| 20 |
+
});
|
| 21 |
+
|
| 22 |
+
res.status(201).json({ token, user: { id: user._id, username, email, role: user.role } });
|
| 23 |
+
} catch (err) {
|
| 24 |
+
res.status(500).json({ message: 'Server error' });
|
| 25 |
+
}
|
| 26 |
+
});
|
| 27 |
+
|
| 28 |
+
// Login
|
| 29 |
+
router.post('/login', async (req, res) => {
|
| 30 |
+
try {
|
| 31 |
+
const { email, password } = req.body;
|
| 32 |
+
const user = await User.findOne({ email });
|
| 33 |
+
|
| 34 |
+
if (!user || !(await user.comparePassword(password))) {
|
| 35 |
+
return res.status(400).json({ message: 'Invalid credentials' });
|
| 36 |
+
}
|
| 37 |
+
|
| 38 |
+
const token = jwt.sign({ id: user._id }, process.env.JWT_SECRET, {
|
| 39 |
+
expiresIn: '30d'
|
| 40 |
+
});
|
| 41 |
+
|
| 42 |
+
res.json({
|
| 43 |
+
token,
|
| 44 |
+
user: {
|
| 45 |
+
id: user._id,
|
| 46 |
+
username: user.username,
|
| 47 |
+
email: user.email,
|
| 48 |
+
role: user.role
|
| 49 |
+
}
|
| 50 |
+
});
|
| 51 |
+
} catch (err) {
|
| 52 |
+
res.status(500).json({ message: 'Server error' });
|
| 53 |
+
}
|
| 54 |
+
});
|
| 55 |
+
|
| 56 |
+
module.exports = router;
|
server/routes/courses.js
ADDED
|
@@ -0,0 +1,60 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
const express = require('express');
|
| 2 |
+
const Course = require('../models/Course');
|
| 3 |
+
const auth = require('../middleware/auth');
|
| 4 |
+
const router = express.Router();
|
| 5 |
+
|
| 6 |
+
// Get all courses
|
| 7 |
+
router.get('/', async (req, res) => {
|
| 8 |
+
try {
|
| 9 |
+
const { category, level, featured } = req.query;
|
| 10 |
+
const query = {};
|
| 11 |
+
|
| 12 |
+
if (category) query.category = category;
|
| 13 |
+
if (level) query.level = level;
|
| 14 |
+
if (featured) query.isFeatured = featured === 'true';
|
| 15 |
+
|
| 16 |
+
const courses = await Course.find(query)
|
| 17 |
+
.populate('instructor', 'username profile.fullName')
|
| 18 |
+
.sort({ createdAt: -1 });
|
| 19 |
+
|
| 20 |
+
res.json(courses);
|
| 21 |
+
} catch (err) {
|
| 22 |
+
res.status(500).json({ message: 'Server error' });
|
| 23 |
+
}
|
| 24 |
+
});
|
| 25 |
+
|
| 26 |
+
// Get single course
|
| 27 |
+
router.get('/:id', async (req, res) => {
|
| 28 |
+
try {
|
| 29 |
+
const course = await Course.findById(req.params.id)
|
| 30 |
+
.populate('instructor', 'username profile.fullName profile.bio profile.avatar')
|
| 31 |
+
.populate('lessons.quiz');
|
| 32 |
+
|
| 33 |
+
if (!course) return res.status(404).json({ message: 'Course not found' });
|
| 34 |
+
|
| 35 |
+
res.json(course);
|
| 36 |
+
} catch (err) {
|
| 37 |
+
res.status(500).json({ message: 'Server error' });
|
| 38 |
+
}
|
| 39 |
+
});
|
| 40 |
+
|
| 41 |
+
// Create course (admin/instructor only)
|
| 42 |
+
router.post('/', auth, async (req, res) => {
|
| 43 |
+
try {
|
| 44 |
+
if (req.user.role !== 'admin' && req.user.role !== 'instructor') {
|
| 45 |
+
return res.status(403).json({ message: 'Not authorized' });
|
| 46 |
+
}
|
| 47 |
+
|
| 48 |
+
const course = new Course({
|
| 49 |
+
...req.body,
|
| 50 |
+
instructor: req.user.id
|
| 51 |
+
});
|
| 52 |
+
|
| 53 |
+
await course.save();
|
| 54 |
+
res.status(201).json(course);
|
| 55 |
+
} catch (err) {
|
| 56 |
+
res.status(500).json({ message: 'Server error' });
|
| 57 |
+
}
|
| 58 |
+
});
|
| 59 |
+
|
| 60 |
+
module.exports = router;
|
server/server.js
ADDED
|
@@ -0,0 +1,39 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
require('dotenv').config();
|
| 2 |
+
const express = require('express');
|
| 3 |
+
const mongoose = require('mongoose');
|
| 4 |
+
const cors = require('cors');
|
| 5 |
+
const path = require('path');
|
| 6 |
+
|
| 7 |
+
const app = express();
|
| 8 |
+
|
| 9 |
+
// Middleware
|
| 10 |
+
app.use(cors());
|
| 11 |
+
app.use(express.json());
|
| 12 |
+
app.use(express.static(path.join(__dirname, '../client/dist')));
|
| 13 |
+
|
| 14 |
+
// Database connection
|
| 15 |
+
mongoose.connect(process.env.MONGODB_URI, {
|
| 16 |
+
useNewUrlParser: true,
|
| 17 |
+
useUnifiedTopology: true
|
| 18 |
+
})
|
| 19 |
+
.then(() => console.log('Connected to MongoDB'))
|
| 20 |
+
.catch(err => console.error('MongoDB connection error:', err));
|
| 21 |
+
|
| 22 |
+
// Routes
|
| 23 |
+
const authRoutes = require('./routes/auth');
|
| 24 |
+
const courseRoutes = require('./routes/courses');
|
| 25 |
+
const userRoutes = require('./routes/users');
|
| 26 |
+
|
| 27 |
+
app.use('/api/auth', authRoutes);
|
| 28 |
+
app.use('/api/courses', courseRoutes);
|
| 29 |
+
app.use('/api/users', userRoutes);
|
| 30 |
+
|
| 31 |
+
// Serve client
|
| 32 |
+
app.get('*', (req, res) => {
|
| 33 |
+
res.sendFile(path.join(__dirname, '../client/dist/index.html'));
|
| 34 |
+
});
|
| 35 |
+
|
| 36 |
+
const PORT = process.env.PORT || 5000;
|
| 37 |
+
app.listen(PORT, () => {
|
| 38 |
+
console.log(`Server running on port ${PORT}`);
|
| 39 |
+
});
|