From 8a740e78477ed1a12511983534528e5331a0bb5a Mon Sep 17 00:00:00 2001 From: "Zhiqi(Eli) Wang" <102864035+ZhiqiEliWang@users.noreply.github.com> Date: Wed, 12 Jul 2023 15:29:52 -0400 Subject: [PATCH] some test code with mongodb and backend test code with mongodb and backend to test connection --- .../backend/package-lock.json | 72 +++++++++++++++++++ campus-connect-react/backend/package.json | 1 + campus-connect-react/backend/server.js | 47 +++++++++++- 3 files changed, 119 insertions(+), 1 deletion(-) diff --git a/campus-connect-react/backend/package-lock.json b/campus-connect-react/backend/package-lock.json index d2267b6..7da022a 100644 --- a/campus-connect-react/backend/package-lock.json +++ b/campus-connect-react/backend/package-lock.json @@ -9,6 +9,7 @@ "version": "1.0.0", "license": "ISC", "dependencies": { + "axios": "^1.4.0", "express": "^4.18.2", "mongodb": "^5.7.0", "nodemon": "^3.0.1" @@ -67,6 +68,21 @@ "resolved": "https://registry.npmjs.org/array-flatten/-/array-flatten-1.1.1.tgz", "integrity": "sha512-PCVAQswWemu6UdxsDFFX/+gVeYqKAod3D3UVm91jHwynguOwAvYPhx8nNlM++NqRcK6CxxpUafjmhIdKiHibqg==" }, + "node_modules/asynckit": { + "version": "0.4.0", + "resolved": "https://registry.npmjs.org/asynckit/-/asynckit-0.4.0.tgz", + "integrity": "sha512-Oei9OH4tRh0YqU3GxhX79dM/mwVgvbZJaSNaRk+bshkj0S5cfHcgYakreBjrHwatXKbz+IoIdYLxrKim2MjW0Q==" + }, + "node_modules/axios": { + "version": "1.4.0", + "resolved": "https://registry.npmjs.org/axios/-/axios-1.4.0.tgz", + "integrity": "sha512-S4XCWMEmzvo64T9GfvQDOXgYRDJ/wsSZc7Jvdgx5u1sd0JwsuPLqb3SYmusag+edF6ziyMensPVqLTSc1PiSEA==", + "dependencies": { + "follow-redirects": "^1.15.0", + "form-data": "^4.0.0", + "proxy-from-env": "^1.1.0" + } + }, "node_modules/balanced-match": { "version": "1.0.2", "resolved": "https://registry.npmjs.org/balanced-match/-/balanced-match-1.0.2.tgz", @@ -177,6 +193,17 @@ "fsevents": "~2.3.2" } }, + "node_modules/combined-stream": { + "version": "1.0.8", + "resolved": "https://registry.npmjs.org/combined-stream/-/combined-stream-1.0.8.tgz", + "integrity": "sha512-FQN4MRfuJeHf7cBbBMJFXhKSDq+2kAArBlmRBvcvFE5BB1HZKXtSFASDhdlz9zOYwxh8lDdnvmMOe/+5cdoEdg==", + "dependencies": { + "delayed-stream": "~1.0.0" + }, + "engines": { + "node": ">= 0.8" + } + }, "node_modules/concat-map": { "version": "0.0.1", "resolved": "https://registry.npmjs.org/concat-map/-/concat-map-0.0.1.tgz", @@ -222,6 +249,14 @@ "ms": "2.0.0" } }, + "node_modules/delayed-stream": { + "version": "1.0.0", + "resolved": "https://registry.npmjs.org/delayed-stream/-/delayed-stream-1.0.0.tgz", + "integrity": "sha512-ZySD7Nf91aLB0RxL4KGrKHBXl7Eds1DAmEdcoVawXnLD7SDhpNgtuII2aAkg7a7QS41jxPSZ17p4VdGnMHk3MQ==", + "engines": { + "node": ">=0.4.0" + } + }, "node_modules/depd": { "version": "2.0.0", "resolved": "https://registry.npmjs.org/depd/-/depd-2.0.0.tgz", @@ -334,6 +369,38 @@ "node": ">= 0.8" } }, + "node_modules/follow-redirects": { + "version": "1.15.2", + "resolved": "https://registry.npmjs.org/follow-redirects/-/follow-redirects-1.15.2.tgz", + "integrity": "sha512-VQLG33o04KaQ8uYi2tVNbdrWp1QWxNNea+nmIB4EVM28v0hmP17z7aG1+wAkNzVq4KeXTq3221ye5qTJP91JwA==", + "funding": [ + { + "type": "individual", + "url": "https://github.com/sponsors/RubenVerborgh" + } + ], + "engines": { + "node": ">=4.0" + }, + "peerDependenciesMeta": { + "debug": { + "optional": true + } + } + }, + "node_modules/form-data": { + "version": "4.0.0", + "resolved": "https://registry.npmjs.org/form-data/-/form-data-4.0.0.tgz", + "integrity": "sha512-ETEklSGi5t0QMZuiXoA/Q6vcnxcLQP5vdugSpuAyi6SVGi2clPPp+xgEhuMaHC+zGgn31Kd235W35f7Hykkaww==", + "dependencies": { + "asynckit": "^0.4.0", + "combined-stream": "^1.0.8", + "mime-types": "^2.1.12" + }, + "engines": { + "node": ">= 6" + } + }, "node_modules/forwarded": { "version": "0.2.0", "resolved": "https://registry.npmjs.org/forwarded/-/forwarded-0.2.0.tgz", @@ -779,6 +846,11 @@ "node": ">= 0.10" } }, + "node_modules/proxy-from-env": { + "version": "1.1.0", + "resolved": "https://registry.npmjs.org/proxy-from-env/-/proxy-from-env-1.1.0.tgz", + "integrity": "sha512-D+zkORCbA9f1tdWRK0RaCR3GPv50cMxcrz4X8k5LTSUD1Dkw47mKJEZQNunItRTkWwgtaUSo1RVFRIG9ZXiFYg==" + }, "node_modules/pstree.remy": { "version": "1.1.8", "resolved": "https://registry.npmjs.org/pstree.remy/-/pstree.remy-1.1.8.tgz", diff --git a/campus-connect-react/backend/package.json b/campus-connect-react/backend/package.json index 1e60aa3..f5abf94 100644 --- a/campus-connect-react/backend/package.json +++ b/campus-connect-react/backend/package.json @@ -9,6 +9,7 @@ "author": "Byte-Force", "license": "ISC", "dependencies": { + "axios": "^1.4.0", "express": "^4.18.2", "mongodb": "^5.7.0", "nodemon": "^3.0.1" diff --git a/campus-connect-react/backend/server.js b/campus-connect-react/backend/server.js index 3413d67..3d5f9ae 100644 --- a/campus-connect-react/backend/server.js +++ b/campus-connect-react/backend/server.js @@ -1,3 +1,48 @@ +//set server.js const express = require('express'); const app = express(); -const port = 3000;app.listen(port, () => console.log(`Server listening on port ${port}!`)); \ No newline at end of file +const port = 3000; +const axios = require('axios'); +const {MongoClient} = require('mongodb'); + +const url = 'mongodb+srv://fengj5:fHg06pjJ5ltsv0G8@cluster0.nrh8keh.mongodb.net/?retryWrites=true&w=majority'; +const client = new MongoClient(url); + +// app.use(express.json()); +// app.use((req, res, next) => { +// res.setHeader("Access-Control-Allow-Origin", "https://fengj5.eastus.cloudapp.azure.com/node"); +// // res.setHeader("Access-Control-Allow-Origin", "http://localhost:5173"); + +// res.setHeader( +// "Access-Control-Allow-Headers", +// "Origin, X-Requested-With, Content-Type, Accept" +// ); +// res.setHeader( +// "Access-Control-Allow-Methods", +// "GET, POST, PATCH, DELETE, OPTIONS, PUT" +// ); + +// next(); +// }); + +app.get('/db', async (req, res) => { + // connect to database and call news api with axios https://newsapi.org/v2/everything?q=us&apiKey=43940c2d527346fda96ecbbee77ca1bb + try{ + await client.connect(); + const database = client.db('CampusConnect'); + // console log all collections name in database + const collections = await database.listCollections().toArray(); + console.log(collections); + //console log the version of mongodb + const serverInfo = await database.command({buildInfo: 1}); + console.log(serverInfo.version); + + } + catch (error) { + console.error(error); + res.status(500).json({ error: 'An error occurred while fetching data.' }); + } finally { + await client.close(); + } +}); +app.listen(port, () => { console.log(`Server listening on port ${port}`); }); \ No newline at end of file