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

MongoDB connection added

parent 22c8834a
This diff is collapsed.
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