Skip to content

Commit

Permalink
Added MongoDB functionality #minor
Browse files Browse the repository at this point in the history
  • Loading branch information
ntuff committed Mar 20, 2024
1 parent ddb93a6 commit 479601f
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 2 deletions.
6 changes: 5 additions & 1 deletion backend/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
6 changes: 5 additions & 1 deletion backend/src/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -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()

Expand Down Expand Up @@ -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)
})
33 changes: 33 additions & 0 deletions backend/src/mongo.js
Original file line number Diff line number Diff line change
@@ -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://<username>:<password>]@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;
1 change: 1 addition & 0 deletions frontend/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down

0 comments on commit 479601f

Please sign in to comment.