From 05fd18e22025cf94d4c528788741f3b61ef85bba Mon Sep 17 00:00:00 2001 From: samratghosh291 Date: Sun, 1 Oct 2023 15:30:02 +0530 Subject: [PATCH 1/2] Set Up Database for Course Information --- package-lock.json | 157 ++++++++++++++++++++++++++++++++++++++++++++- package.json | 3 +- src/db/courseDB.js | 30 +++++++++ 3 files changed, 188 insertions(+), 2 deletions(-) create mode 100644 src/db/courseDB.js diff --git a/package-lock.json b/package-lock.json index 95e1253..37ea326 100644 --- a/package-lock.json +++ b/package-lock.json @@ -7,7 +7,8 @@ "dependencies": { "ejs": "^3.1.9", "express": "^4.18.2", - "mongodb": "^6.1.0" + "mongodb": "^6.1.0", + "mongoose": "^7.5.3" } }, "node_modules/@mongodb-js/saslprep": { @@ -463,6 +464,11 @@ "resolved": "https://registry.npmjs.org/inherits/-/inherits-2.0.4.tgz", "integrity": "sha512-k/vGaX4/Yla3WzyMCvTQOXYeIHvqOKtnqBduzTHpzpQZzAskKMhZ2K+EnBiSM9zGSoIFeMpXKxa4dYeZIQqewQ==" }, + "node_modules/ip": { + "version": "2.0.0", + "resolved": "https://registry.npmjs.org/ip/-/ip-2.0.0.tgz", + "integrity": "sha512-WKa+XuLG1A1R0UWhl2+1XQSi+fZWMsYKffMZTTYsiZaUD8k2yDAj5atimTUD2TZkyCkNEeYE5NhFZmupOGtjYQ==" + }, "node_modules/ipaddr.js": { "version": "1.9.1", "resolved": "https://registry.npmjs.org/ipaddr.js/-/ipaddr.js-1.9.1.tgz", @@ -488,6 +494,14 @@ "node": ">=10" } }, + "node_modules/kareem": { + "version": "2.5.1", + "resolved": "https://registry.npmjs.org/kareem/-/kareem-2.5.1.tgz", + "integrity": "sha512-7jFxRVm+jD+rkq3kY0iZDJfsO2/t4BBPeEb2qKn2lR/9KhuksYk5hxzfRYWMPV8P/x2d0kHD306YyWLzjjH+uA==", + "engines": { + "node": ">=12.0.0" + } + }, "node_modules/media-typer": { "version": "0.3.0", "resolved": "https://registry.npmjs.org/media-typer/-/media-typer-0.3.0.tgz", @@ -609,6 +623,120 @@ "whatwg-url": "^11.0.0" } }, + "node_modules/mongoose": { + "version": "7.5.3", + "resolved": "https://registry.npmjs.org/mongoose/-/mongoose-7.5.3.tgz", + "integrity": "sha512-QyYzhZusux0wIJs+4rYyHvel0kJm0CT887trNd1WAB3iQnDuJow0xEnjETvuS/cTjHQUVPihOpN7OHLlpJc52w==", + "dependencies": { + "bson": "^5.4.0", + "kareem": "2.5.1", + "mongodb": "5.8.1", + "mpath": "0.9.0", + "mquery": "5.0.0", + "ms": "2.1.3", + "sift": "16.0.1" + }, + "engines": { + "node": ">=14.20.1" + }, + "funding": { + "type": "opencollective", + "url": "https://opencollective.com/mongoose" + } + }, + "node_modules/mongoose/node_modules/bson": { + "version": "5.5.0", + "resolved": "https://registry.npmjs.org/bson/-/bson-5.5.0.tgz", + "integrity": "sha512-B+QB4YmDx9RStKv8LLSl/aVIEV3nYJc3cJNNTK2Cd1TL+7P+cNpw9mAPeCgc5K+j01Dv6sxUzcITXDx7ZU3F0w==", + "engines": { + "node": ">=14.20.1" + } + }, + "node_modules/mongoose/node_modules/mongodb": { + "version": "5.8.1", + "resolved": "https://registry.npmjs.org/mongodb/-/mongodb-5.8.1.tgz", + "integrity": "sha512-wKyh4kZvm6NrCPH8AxyzXm3JBoEf4Xulo0aUWh3hCgwgYJxyQ1KLST86ZZaSWdj6/kxYUA3+YZuyADCE61CMSg==", + "dependencies": { + "bson": "^5.4.0", + "mongodb-connection-string-url": "^2.6.0", + "socks": "^2.7.1" + }, + "engines": { + "node": ">=14.20.1" + }, + "optionalDependencies": { + "@mongodb-js/saslprep": "^1.1.0" + }, + "peerDependencies": { + "@aws-sdk/credential-providers": "^3.188.0", + "@mongodb-js/zstd": "^1.0.0", + "kerberos": "^1.0.0 || ^2.0.0", + "mongodb-client-encryption": ">=2.3.0 <3", + "snappy": "^7.2.2" + }, + "peerDependenciesMeta": { + "@aws-sdk/credential-providers": { + "optional": true + }, + "@mongodb-js/zstd": { + "optional": true + }, + "kerberos": { + "optional": true + }, + "mongodb-client-encryption": { + "optional": true + }, + "snappy": { + "optional": true + } + } + }, + "node_modules/mongoose/node_modules/ms": { + "version": "2.1.3", + "resolved": "https://registry.npmjs.org/ms/-/ms-2.1.3.tgz", + "integrity": "sha512-6FlzubTLZG3J2a/NVCAleEhjzq5oxgHyaCU9yYXvcLsvoVaHJq/s5xXI6/XXP6tz7R9xAOtHnSO/tXtF3WRTlA==" + }, + "node_modules/mpath": { + "version": "0.9.0", + "resolved": "https://registry.npmjs.org/mpath/-/mpath-0.9.0.tgz", + "integrity": "sha512-ikJRQTk8hw5DEoFVxHG1Gn9T/xcjtdnOKIU1JTmGjZZlg9LST2mBLmcX3/ICIbgJydT2GOc15RnNy5mHmzfSew==", + "engines": { + "node": ">=4.0.0" + } + }, + "node_modules/mquery": { + "version": "5.0.0", + "resolved": "https://registry.npmjs.org/mquery/-/mquery-5.0.0.tgz", + "integrity": "sha512-iQMncpmEK8R8ncT8HJGsGc9Dsp8xcgYMVSbs5jgnm1lFHTZqMJTUWTDx1LBO8+mK3tPNZWFLBghQEIOULSTHZg==", + "dependencies": { + "debug": "4.x" + }, + "engines": { + "node": ">=14.0.0" + } + }, + "node_modules/mquery/node_modules/debug": { + "version": "4.3.4", + "resolved": "https://registry.npmjs.org/debug/-/debug-4.3.4.tgz", + "integrity": "sha512-PRWFHuSU3eDtQJPvnNY7Jcket1j0t5OuOsFzPPzsekD52Zl8qUfFIPEiswXqIvHWGVHOgX+7G/vCNNhehwxfkQ==", + "dependencies": { + "ms": "2.1.2" + }, + "engines": { + "node": ">=6.0" + }, + "peerDependenciesMeta": { + "supports-color": { + "optional": true + } + } + }, + "node_modules/mquery/node_modules/ms": { + "version": "2.1.2", + "resolved": "https://registry.npmjs.org/ms/-/ms-2.1.2.tgz", + "integrity": "sha512-sGkPx+VjMtmA6MX27oA4FBFELFCZZ4S4XqeGOXCv68tT+jb3vk/RyaKWP0PTKyWtmLSM0b+adUTEvbs1PEaH2w==" + }, "node_modules/ms": { "version": "2.0.0", "resolved": "https://registry.npmjs.org/ms/-/ms-2.0.0.tgz", @@ -794,6 +922,33 @@ "url": "https://github.com/sponsors/ljharb" } }, + "node_modules/sift": { + "version": "16.0.1", + "resolved": "https://registry.npmjs.org/sift/-/sift-16.0.1.tgz", + "integrity": "sha512-Wv6BjQ5zbhW7VFefWusVP33T/EM0vYikCaQ2qR8yULbsilAT8/wQaXvuQ3ptGLpoKx+lihJE3y2UTgKDyyNHZQ==" + }, + "node_modules/smart-buffer": { + "version": "4.2.0", + "resolved": "https://registry.npmjs.org/smart-buffer/-/smart-buffer-4.2.0.tgz", + "integrity": "sha512-94hK0Hh8rPqQl2xXc3HsaBoOXKV20MToPkcXvwbISWLEs+64sBq5kFgn2kJDHb1Pry9yrP0dxrCI9RRci7RXKg==", + "engines": { + "node": ">= 6.0.0", + "npm": ">= 3.0.0" + } + }, + "node_modules/socks": { + "version": "2.7.1", + "resolved": "https://registry.npmjs.org/socks/-/socks-2.7.1.tgz", + "integrity": "sha512-7maUZy1N7uo6+WVEX6psASxtNlKaNVMlGQKkG/63nEDdLOWNbiUMoLK7X4uYoLhQstau72mLgfEWcXcwsaHbYQ==", + "dependencies": { + "ip": "^2.0.0", + "smart-buffer": "^4.2.0" + }, + "engines": { + "node": ">= 10.13.0", + "npm": ">= 3.0.0" + } + }, "node_modules/sparse-bitfield": { "version": "3.0.3", "resolved": "https://registry.npmjs.org/sparse-bitfield/-/sparse-bitfield-3.0.3.tgz", diff --git a/package.json b/package.json index 40104ac..72250c0 100644 --- a/package.json +++ b/package.json @@ -2,6 +2,7 @@ "dependencies": { "ejs": "^3.1.9", "express": "^4.18.2", - "mongodb": "^6.1.0" + "mongodb": "^6.1.0", + "mongoose": "^7.5.3" } } diff --git a/src/db/courseDB.js b/src/db/courseDB.js new file mode 100644 index 0000000..7bf7a3a --- /dev/null +++ b/src/db/courseDB.js @@ -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; From 314ee5518d005719c325d2ca5bac80a8bfe4c2ab Mon Sep 17 00:00:00 2001 From: samratghosh291 Date: Sun, 1 Oct 2023 16:06:18 +0530 Subject: [PATCH 2/2] add login function in main .js and also add config.json --- config.json | 5 +++++ src/app.js | 58 ++++++++++++++++++++++++++++++++++++++++++++++++----- 2 files changed, 58 insertions(+), 5 deletions(-) create mode 100644 config.json diff --git a/config.json b/config.json new file mode 100644 index 0000000..4a67cec --- /dev/null +++ b/config.json @@ -0,0 +1,5 @@ +{ + "mongodb_uri": "YOUR_MONGODB_URI_HERE", + "secret_key": "YOUR_SECRET_KEY_HERE" + } + \ No newline at end of file diff --git a/src/app.js b/src/app.js index d4a34f8..dc153b1 100644 --- a/src/app.js +++ b/src/app.js @@ -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}`); });