-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add the feature to Set Up Database for Course Information (#19)
- Loading branch information
Showing
5 changed files
with
246 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
{ | ||
"mongodb_uri": "YOUR_MONGODB_URI_HERE", | ||
"secret_key": "YOUR_SECRET_KEY_HERE" | ||
} | ||
|
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,59 @@ | ||
const express = require("express"); | ||
const express = require('express'); | ||
const mongoose = require('mongoose'); | ||
const fs = require('fs'); | ||
|
||
const app = express(); | ||
|
||
app.get("/", (req, res) => { | ||
res.send("Hello World!"); | ||
// Read configuration from config.json | ||
let config; | ||
try { | ||
const configData = fs.readFileSync('config.json'); | ||
config = JSON.parse(configData); | ||
} catch (err) { | ||
console.error('Error reading config file:', err); | ||
process.exit(1); | ||
} | ||
|
||
// Connect to MongoDB using the configuration | ||
mongoose.connect(config.mongodb_uri, { | ||
useNewUrlParser: true, | ||
useUnifiedTopology: true, | ||
}) | ||
.then(() => { | ||
console.log('Connected to MongoDB'); | ||
// Start your application logic here | ||
}) | ||
.catch((err) => { | ||
console.error('Error connecting to MongoDB:', err); | ||
process.exit(1); | ||
}); | ||
|
||
// Simulated user data (replace with actual user data from your database) | ||
const users = [ | ||
{ username: 'user1', password: 'password1' }, | ||
{ username: 'user2', password: 'password2' }, | ||
]; | ||
|
||
// Define your login function here | ||
function login(username, password) { | ||
const user = users.find((u) => u.username === username && u.password === password); | ||
return !!user; // Return true if the user exists, false otherwise | ||
} | ||
|
||
// Example usage of the login function | ||
app.get('/login', (req, res) => { | ||
const username = req.query.username; | ||
const password = req.query.password; | ||
|
||
if (login(username, password)) { | ||
res.send('Login successful'); | ||
} else { | ||
res.status(401).send('Login failed'); | ||
} | ||
}); | ||
|
||
app.listen(3000, () => { | ||
console.log("Server is up on port 3000"); | ||
// Start the server | ||
const PORT = process.env.PORT || 3000; | ||
app.listen(PORT, () => { | ||
console.log(`Server is running on port ${PORT}`); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
// Import the mongoose library | ||
const mongoose = require('mongoose'); | ||
|
||
// Define a schema for the course | ||
const courseSchema = new mongoose.Schema({ | ||
title: { | ||
type: String, | ||
required: true, | ||
trim: true, // Remove whitespace from the beginning and end of the string | ||
}, | ||
description: { | ||
type: String, | ||
required: true, | ||
}, | ||
duration: { | ||
type: Number, | ||
required: true, | ||
}, | ||
difficulty: { | ||
type: String, | ||
required: true, | ||
}, | ||
// Add more fields as needed | ||
}); | ||
|
||
// Create a model using the schema | ||
const Course = mongoose.model('Course', courseSchema); | ||
|
||
// Export the Course model | ||
module.exports = Course; |