From 479601ffe003834ff6ae9f57f6732aaff9c14a3d Mon Sep 17 00:00:00 2001 From: ntuff Date: Wed, 20 Mar 2024 21:01:24 -0230 Subject: [PATCH] Added MongoDB functionality #minor --- backend/package.json | 6 +++++- backend/src/app.js | 6 +++++- backend/src/mongo.js | 33 +++++++++++++++++++++++++++++++++ frontend/package.json | 1 + 4 files changed, 44 insertions(+), 2 deletions(-) create mode 100644 backend/src/mongo.js diff --git a/backend/package.json b/backend/package.json index 1de1488..e34b7e2 100644 --- a/backend/package.json +++ b/backend/package.json @@ -2,14 +2,18 @@ "name": "backend", "version": "1.0.0", "description": "", - "main": "index.js", + "main": "app.js", "scripts": { + "start": "node --env-file=.env src/app.js", + "dev": "nodemon --env-file=.env src/app.js", "test": "echo \"Error: no test specified\" && exit 1" }, "author": "", "license": "ISC", "dependencies": { + "dotenv": "^16.4.5", "express": "^4.18.3", + "jsonwebtoken": "^9.0.2", "mongodb": "^6.5.0" }, "type": "module" diff --git a/backend/src/app.js b/backend/src/app.js index f8a684a..5dadd58 100644 --- a/backend/src/app.js +++ b/backend/src/app.js @@ -2,7 +2,10 @@ import express from 'express'; import path from 'path'; import { fileURLToPath } from 'url'; import bodyParser from 'body-parser'; -import jwt from 'jsonwebtoken'; // TODO - add a jsonwebtoken package or module +// import jwt from 'jsonwebtoken'; // TODO - add a jsonwebtoken package or module +import * as jwt from 'jsonwebtoken' +import mongo from "./mongo.js"; +//import 'dotenv/config' const app = express() @@ -31,4 +34,5 @@ app.get(/^(?!\/api).+/, (req, res) => { app.listen(port, () => { console.log(`Predictive Vehicle Maintenance app listening on port ${port}`) + console.log(mongo) }) \ No newline at end of file diff --git a/backend/src/mongo.js b/backend/src/mongo.js new file mode 100644 index 0000000..3a4e6ae --- /dev/null +++ b/backend/src/mongo.js @@ -0,0 +1,33 @@ +//https://www.mongodb.com/docs/drivers/node/current/fundamentals/connection/connect/#std-label-node-connect-to-mongodb + +import { MongoClient, ServerApiVersion } from "mongodb"; +import 'dotenv/config' + + +const uri = process.env.MONGO_URI; +// const uri ='mongodb+srv://:]@driveline-cluster-01.kiepke5.mongodb.net/' + +// Create a MongoClient with a MongoClientOptions object to set the Stable API version +const client = new MongoClient(uri, { + serverApi: { + version: ServerApiVersion.v1, + strict: true, + deprecationErrors: true, + } + } +); +async function run() { + try { + // Connect the client to the server (optional starting in v4.7) + await client.connect(); + // Send a ping to confirm a successful connection + await client.db("admin").command({ ping: 1 }); + console.log("Pinged your deployment. You successfully connected to MongoDB!"); + } finally { + // Ensures that the client will close when you finish/error + //await client.close(); + } +} +run().catch(console.dir); + +export default client; \ No newline at end of file diff --git a/frontend/package.json b/frontend/package.json index 0fdab99..2ad98da 100644 --- a/frontend/package.json +++ b/frontend/package.json @@ -6,6 +6,7 @@ "@testing-library/jest-dom": "^5.17.0", "@testing-library/react": "^13.4.0", "@testing-library/user-event": "^13.5.0", + "mongodb": "^6.5.0", "react": "^18.2.0", "react-dom": "^18.2.0", "react-router-dom": "^6.22.3",