└── README.md /README.md: -------------------------------------------------------------------------------- 1 | ## Basic Setup 2 | 3 | [Got to top](#let-start-express) 4 | 5 | **Embedded on `index.jx` to Connect.** 6 | 7 | ```Js 8 | const express = require("express"); 9 | const cors = require("cors"); 10 | const { MongoClient, ServerApiVersion, ObjectId } = require("mongodb"); 11 | const jwt = require("jsonwebtoken"); // import jwt if use JsonWeb token 12 | require("dotenv").config(); // import env file if use environment variables after install || npm install dotenv --save|| ane Create a .env file in the root of your project: 13 | const port = process.env.PORT || 5000; 14 | const app = express(); 15 | // used Middleware 16 | app.use(cors()); 17 | app.use(express.json()); 18 | // Connact With MongoDb Database 19 | const uri = `mongodb+srv://${process.env.DB_USER}:${process.env.DB_PASS}@cluster0.krxly.mongodb.net/?retryWrites=true&w=majority`; 20 | const client = new MongoClient(uri, { 21 | useNewUrlParser: true, 22 | useUnifiedTopology: true, 23 | serverApi: ServerApiVersion.v1, 24 | }); 25 | // Create a async fucntion to all others activity 26 | async function run() { 27 | try { 28 | await client.connect(); 29 | // Create Database to store Data 30 | const testCollection = client.db("testDatabaseName").collection("testData"); 31 | } finally { 32 | // await client.close(); 33 | } 34 | } 35 | // Call the function you declare abobe 36 | run().catch(console.dir); 37 | // Root Api to check activity 38 | app.get("/", (req, res) => { 39 | res.send("Hello From NR Computers!"); 40 | }); 41 | app.listen(port, () => { 42 | console.log(`NR Computers listening on port ${port}`); 43 | }); 44 | ``` 45 | 46 | ## Get Method 47 | 48 | [Got to top](#let-start-express) 49 | 50 | ### Get/Access all data from MongoDB database 51 | 52 | `Server Site Code` 53 | 54 | ```Js 55 | app.get("/service", async (req, res) => { 56 | const result = await testCollection.find().toArray(); 57 | res.send(result); 58 | }); 59 | ``` 60 | 61 | `Client site code` 62 | 63 | ```Js 64 | useEffect(() => { 65 | fetch("http://localhost:5000/service") 66 | .then((res) => res.json()) 67 | .then((data) => console.log(data)); 68 | }, []); 69 | ``` 70 | 71 | ## Get/Access Specific data from Mongodb database with id/email 72 | 73 | `Server Site Code` 74 | 75 | ```Js 76 | app.get("/service/:id", async (req, res) => { 77 | const id = req.params.id; 78 | const result = await testCollection.find({ _id: new ObjectId(id) }).toArray(); 79 | res.send(result); 80 | }); 81 | ``` 82 | 83 | `Client site code` 84 | 85 | ```Js 86 | useEffect(() => { 87 | fetch(`http://localhost:5000/service/${id}`) 88 | .then((res) => res.json()) 89 | .then((data) => console.log(data)); 90 | }, []); 91 | ``` 92 | 93 | ## Get/Access Specific data from MongoDB database with multiple queries 94 | 95 | `Server Site Code` 96 | 97 | ```Js 98 | app.get("/service", async (req, res) => { 99 | const query = req.body; 100 | const result = await testCollection.find(query).toArray(); 101 | res.send(result); 102 | }); 103 | ``` 104 | 105 | `Client site code` 106 | 107 | ```Js 108 | const query = { 109 | email: "exampale@example.com", 110 | name: "set name", 111 | time: "set time", 112 | }; 113 | useEffect(() => { 114 | fetch("http://localhost:5000/service", { 115 | method: "GET", 116 | body: JSON.stringify(query), // send any of query by which you find data 117 | }) 118 | .then((res) => res.json()) 119 | .then((data) => console.log(data)); 120 | }, []); 121 | ``` 122 | 123 | ## Get/Access Limited data from the MongoDB database after finding the query 124 | 125 | `Server Site Code` 126 | 127 | ```Js 128 | app.get("/service", async (req, res) => { 129 | const query = {}; 130 | const findData = testCollection.find(query); 131 | const result = await findData.skip(5).limit(3).toArray(); // you can also set skip & limit range dynamicly. 132 | res.send(result); 133 | }); 134 | ``` 135 | 136 | `Client site code` 137 | 138 | ```Js 139 | useEffect(() => { 140 | fetch("http://localhost:5000/service") 141 | .then((res) => res.json()) 142 | .then((data) => console.log(data)); 143 | }, []); 144 | ``` 145 | 146 | ## Post Method 147 | 148 | [Got to top](#let-start-express) 149 | 150 | ### Post data without checking on MongoDB database 151 | 152 | `Server Site Code` 153 | 154 | ```Js 155 | app.post("/service", async (req, res) => { 156 | const service = req.body; 157 | const result = await testCollection.insertOne(service); 158 | res.send(result); 159 | }); 160 | ``` 161 | 162 | `Client site code` 163 | 164 | ```Js 165 | const newData = { 166 | // store new Data in Object here 167 | name: "", 168 | price: "", 169 | }; 170 | fetch("http://localhost:5000/service", { 171 | method: "POST", 172 | headers: { 173 | "content-type": "application/json", 174 | }, 175 | body: JSON.stringify(newData), 176 | }) 177 | .then((res) => res.json()) 178 | .then((result) => { 179 | const allData = [...previusData, newData]; 180 | console.log(newReview); 181 | }); 182 | ``` 183 | 184 | ### Post data With check-in Mongodb database. Batter to use Put Methood for this task 185 | 186 | `Server Site Code` 187 | 188 | ```Js 189 | app.post("/service/:email", async (req, res) => { 190 | const service = req.body.service; 191 | const query = req.body.newQuery; 192 | query.email = email; 193 | const exist = testCollection.findOne(query); 194 | if (exist) { 195 | res.send({ result: "success" }); 196 | } else { 197 | const result = await testCollection.insertOne(service); 198 | res.send(result); 199 | } 200 | }); 201 | ``` 202 | 203 | `Client Site Code` 204 | 205 | ```Js 206 | const service = { 207 | name: "", 208 | price: "", 209 | }; 210 | const newQuery = { 211 | name: "", 212 | time: "", 213 | }; 214 | fetch("http://localhost:5000/service", { 215 | method: "POST", 216 | headers: { 217 | "content-type": "application/json", 218 | }, 219 | body: JSON.stringify({ service, newQuery }), 220 | }) 221 | .then((res) => res.json()) 222 | .then((result) => { 223 | const allData = [...previusData, newData]; 224 | console.log(newReview); 225 | }); 226 | ``` 227 | 228 | ## Put Method 229 | 230 | [Got to top](#let-start-express) 231 | 232 | ### Update & insert Data on the database by id/email/otherQuery 233 | 234 | `Server site Code` 235 | 236 | ```Js 237 | app.put("/service/:id", async (req, res) => { 238 | const id = req.params.id; 239 | const service = req.body; 240 | const result = await testCollection.updateOne( 241 | { _id: new ObjectId(id) }, // Find Data by query many time query type is "_id: id" Cheack on database 242 | { 243 | $set: service, // Set updated Data 244 | }, 245 | { upsert: true } // define work 246 | ); 247 | res.send({ result }); 248 | }); 249 | ``` 250 | 251 | `Client Site Code` 252 | 253 | ```Js 254 | const updatedData = { 255 | name: "", 256 | role: "", 257 | }; 258 | fetch(`http://localhost:5000/service/${id}`, { 259 | method: "PUT", 260 | headers: { 261 | "content-type": "application/json", 262 | }, 263 | body: JSON.stringify(updatedData), 264 | }) 265 | .then((res) => res.json()) 266 | .then((data) => { 267 | alert("item Updated successfully!!!"); 268 | }); 269 | ``` 270 | 271 | ## Patch Method 272 | 273 | [Got to top](#let-start-express) 274 | 275 | ### update Data by id/email/othersQueary 276 | 277 | `Server site code` 278 | 279 | ```Js 280 | app.patch("/service/:id", async (req, res) => { 281 | const id = req.params.id; 282 | const service = req.body; 283 | const result = await testCollection.updateOne( 284 | { _id: id }, // sometime _id: new ObjectId(id) 285 | { 286 | $set: service, 287 | } 288 | ); 289 | res.send(result); 290 | }); 291 | ``` 292 | 293 | `Client site code` 294 | 295 | ```Js 296 | const updatedData = { 297 | name: "", 298 | role: "", 299 | }; 300 | fetch(`http://localhost:5000/service/${id}`, { 301 | method: "patch", 302 | headers: { 303 | "content-type": "application/json", 304 | }, 305 | body: JSON.stringify(updatedData), 306 | }) 307 | .then((res) => res.json()) 308 | .then((data) => { 309 | alert("item Updated successfully!!!"); 310 | }); 311 | ``` 312 | 313 | ## Delete Method 314 | 315 | [Got to top](#let-start-express) 316 | 317 | ### Delete Data from MongoDB Database by id 318 | 319 | `Server site code` 320 | 321 | ```Js 322 | app.delete("/service/:id", async (req, res) => { 323 | const id = req.params.id; 324 | const result = await testCollection.deleteOne({ _id: new ObjectId(id) }); 325 | res.send(result); 326 | }); 327 | ``` 328 | 329 | `Client Site Code` 330 | 331 | ```Js 332 | fetch(`http://localhost:5000/service/${_id}`, { 333 | method: "DELETE", 334 | }) 335 | .then((res) => res.json()) 336 | .then((data) => {}); 337 | ``` 338 | 339 | ## Count the number of arrays 340 | 341 | ```Js 342 | app.get("/billsCount", async (req, res) => { 343 | const count = await billCollection.countDocuments(); 344 | res.send({ count }); 345 | }); 346 | 347 | ``` 348 |
349 |

350 | Portfolio → Discord → 351 |

352 | 353 | --------------------------------------------------------------------------------