{"id":20401207,"url":"https://github.com/sohanemon/backend-integration","last_synced_at":"2026-04-08T16:02:18.134Z","repository":{"id":151083398,"uuid":"560459216","full_name":"sohanemon/backend-integration","owner":"sohanemon","description":null,"archived":false,"fork":false,"pushed_at":"2022-11-06T16:43:24.000Z","size":699,"stargazers_count":1,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-08-29T08:44:06.870Z","etag":null,"topics":["crud","express","fetch","mongodb","mongodb-atlas","node","react","react-router"],"latest_commit_sha":null,"homepage":"https://backend-integration.netlify.app/","language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/sohanemon.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2022-11-01T14:49:21.000Z","updated_at":"2022-12-30T14:53:32.000Z","dependencies_parsed_at":null,"dependency_job_id":"69a909ab-6d20-4956-a0c7-7368d2ab4fc7","html_url":"https://github.com/sohanemon/backend-integration","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/sohanemon/backend-integration","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sohanemon%2Fbackend-integration","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sohanemon%2Fbackend-integration/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sohanemon%2Fbackend-integration/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sohanemon%2Fbackend-integration/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/sohanemon","download_url":"https://codeload.github.com/sohanemon/backend-integration/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sohanemon%2Fbackend-integration/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":31562697,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-04-08T14:31:17.711Z","status":"ssl_error","status_checked_at":"2026-04-08T14:31:17.202Z","response_time":54,"last_error":"SSL_connect returned=1 errno=0 peeraddr=140.82.121.5:443 state=error: unexpected eof while reading","robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":false,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["crud","express","fetch","mongodb","mongodb-atlas","node","react","react-router"],"created_at":"2024-11-15T04:48:34.479Z","updated_at":"2026-04-08T16:02:18.119Z","avatar_url":"https://github.com/sohanemon.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"## Server/Backend deploy config\n```js\n{ // vercel.json\n  \"builds\": [{ \"src\": \"index.js\", \"use\": \"@vercel/node\" }],\n  \"routes\": [{ \"src\": \"/(.*)\", \"dest\": \"/index.js\" }]\n}\n```\n## Query vs Params\nBest practice for RESTful API design is that path params are used to identify a specific resource or resources, while query parameters are used to sort/filter those resources.\n```js\nhttp://localhost:5000/orders/?email=mr.es20x@gmail.com\n// are same\nhttp://localhost:5000/orders?email=mr.es20x@gmail.com\n```\n## Posted data is undefined?\n\n- add\n\n```js\napp.use(express.json());\n```\n\n- this will convert the json into a js object\n\n## Posting sample\n\n```js\nconst postStudent = (name) =\u003e {\n  fetch(\"https://backend-integration-test.vercel.app/register\", {\n    method: \"POST\",\n    headers: {\n      \"Content-Type\": \"application/json\",\n    },\n    body: JSON.stringify({ name: name }),\n  })\n    .then((res) =\u003e res.json())\n    .then((data) =\u003e setStudents(data));\n};\n```\n\n# MongoDB\n\n- get the mongoDB_URI. It may looks like `mongodb+srv://sohanemon:\u003cpassword\u003e@\u003cclusterName\u003e.2gtvyou.mongodb.net/?retryWrites=true\u0026w=majority` or `mongodb://localhost:8000/\"`\n- then set as a client\n\n```js\nconst { MongoClient } = require(\"mongodb\");\n// step: 1\nconst client = new MongoClient(process.env.MONGODB_URI);\n```\n\n- retrieve the collection from database\n\n```js\ntry {\n  // step: 2\n  const collection = client.db(\"\u003cmyDbName\").collection(\"\u003cmyCollectionName\u003e\");\n  // myCollectionName collection is inside the myDbName database\n} finally {\n}\n```\n\n- operate different operations on collection variable now\n\n## Insert/Create/Post\n\n```js\nconst runMongo = async () =\u003e {\n  try {\n    const collection = client.db(\"users\").collection(\"students\");\n    const doc = {\n      name: \"rafi\",\n    };\n    // step: 3\n    collection.insertOne(doc);\n  } finally {\n  }\n};\n```\n\n\u003e make sure to run `runMongo()`\n\n\u003e as it is async then we can use `runMongo().catch(console.log)`\n\n- now send the fetched data from frontend to the server as\n\n```js\ntry {\n    const collection = client.db(\"users\").collection(\"students\");\n    app.post(\"/register\", async (req, res) =\u003e {\n      const doc = req.body;\n      // step: 3\n      const result = await collection.insertOne(doc);\n      res.send(result);\n    });\n```\n\n## Get/Read\n\n- `find()` / `findOne` takes `query` and `options` as parameter\n\n```js\ntry {\n    const collection = client.db(\"users\").collection(\"students\");\n\n    app.get(\"/students\", async (req, res) =\u003e {\n      // step: 3\n      const cursor = collection.find({});\n      // await is not required here bcoz find returns a cursor not a promise\n      // step: 4\n      const result = await cursor.toArray();\n      // .toArray() returns a promise to await is required\n      res.json(result);\n    });\n\n  }...\n```\n\n- `findOne` doesn't return any cursor. So, `await` should be used with `findOne` function as it returns a promise\n- by hovering on any function, vs code shows what actually a function return like [this](./src/assets/Screenshot%20from%202022-11-01%2019-28-35.png). Anything after `:` or `=\u003e` determines what actually returning there.\n\n```js\napp.get(\"/student/:_id\", async (req, res) =\u003e {\n  const _id = req.params._id;\n  const result = await collection.findOne({ _id: ObjectId(_id) });\n  res.json(result);\n});\n```\n\n## Update/Put/Patch\n\n- Similar to other it also have `updateOne` and `updateMany` function\n\n```js\napp.put(\"/update/:_id\", async (req, res) =\u003e {\n  const _id = req.params._id;\n  const doc = req.body;\n  const result = await collection.updateOne(\n    { _id: ObjectId(_id) },\n    {\n      $set: doc,\n    }\n  );\n  res.send(result);\n});\n```\n\n## Delete\n\n- `deleteOne` function takes a query argument. Which describes about the items.\n- for deleting an item using \\_id one should pass using `ObjectId` function.\n\n```js\napp.delete(\"/delete/:_id\", async (req, res) =\u003e {\n  const _id = req.params._id;\n  // step: 4\n  const result = await collection.deleteOne({ _id: ObjectId(_id) });\n  res.send(result);\n});\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsohanemon%2Fbackend-integration","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fsohanemon%2Fbackend-integration","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsohanemon%2Fbackend-integration/lists"}