Commit a08e1767 authored by Tharaka it19975696's avatar Tharaka it19975696 🎧

MongoDB connection added

parent 22c8834a
This diff is collapsed.
......@@ -7,6 +7,10 @@
"@testing-library/jest-dom": "^5.17.0",
"@testing-library/react": "^13.4.0",
"@testing-library/user-event": "^13.5.0",
"axios": "^1.6.8",
"dotenv": "^16.4.5",
"express": "^4.19.2",
"mongoose": "^8.3.1",
"react": "^18.2.0",
"react-dom": "^18.2.0",
"react-router-dom": "^6.22.3",
......@@ -40,5 +44,6 @@
},
"devDependencies": {
"@babel/plugin-proposal-private-property-in-object": "^7.21.11"
}
},
"proxy": "http://localhost:5000"
}
DB_USERNAME=R24-152
DB_PASSWORD=1234!@#$Qw
\ No newline at end of file
require('dotenv').config(); // Make sure to require dotenv as early as possible
const express = require('express');
const mongoose = require('mongoose');
const app = express();
// Use JSON middleware to parse JSON bodies into JS objects
app.use(express.json());
// Connect to MongoDB
const username = encodeURIComponent(process.env.DB_USERNAME);
const password = encodeURIComponent(process.env.DB_PASSWORD);
const dbCluster = process.env.DB_CLUSTER;
const dbURI = `mongodb+srv://${username}:${password}@${dbCluster}/myDatabaseName?retryWrites=true&w=majority`;
mongoose.connect(dbURI, { useNewUrlParser: true, useUnifiedTopology: true })
.then(() => console.log('MongoDB connected'))
.catch(err => console.error('Connection error:', err));
// Define routes for your API
app.get('/', (req, res) => {
res.send('Hello World!');
});
// Example POST route
app.post('/login', (req, res) => {
// This is an example, you would have actual logic to verify credentials
const { username, password } = req.body;
if (username === 'teacher' && password === '1234') {
res.json({ status: 'ok', message: 'Teacher logged in' });
} else if (username === 'student' && password === '1234') {
res.json({ status: 'ok', message: 'Student logged in' });
} else {
res.status(401).json({ status: 'error', message: 'Invalid credentials' });
}
});
// Start the server
const PORT = process.env.PORT || 5000;
app.listen(PORT, () => {
console.log(`Server running on port ${PORT}`);
});
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment