{"id":20686218,"url":"https://github.com/sunilksamanta/node-mongoose-setup","last_synced_at":"2025-04-22T13:47:44.899Z","repository":{"id":37528848,"uuid":"293209727","full_name":"sunilksamanta/node-mongoose-setup","owner":"sunilksamanta","description":"Nodejs MongoDB REST API Sarter","archived":false,"fork":false,"pushed_at":"2022-06-23T11:03:34.000Z","size":248,"stargazers_count":63,"open_issues_count":2,"forks_count":17,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-03-29T15:41:36.463Z","etag":null,"topics":["express","hacktoberfest2020","mongodb","mongoose","nodejs","rest-api"],"latest_commit_sha":null,"homepage":"","language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/sunilksamanta.png","metadata":{"files":{"readme":"readme.md","changelog":null,"contributing":"contributing.md","funding":null,"license":"LICENSE","code_of_conduct":"CODE_OF_CONDUCT.md","threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2020-09-06T05:25:56.000Z","updated_at":"2024-10-22T19:10:53.000Z","dependencies_parsed_at":"2022-07-12T16:21:52.720Z","dependency_job_id":null,"html_url":"https://github.com/sunilksamanta/node-mongoose-setup","commit_stats":null,"previous_names":[],"tags_count":0,"template":true,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sunilksamanta%2Fnode-mongoose-setup","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sunilksamanta%2Fnode-mongoose-setup/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sunilksamanta%2Fnode-mongoose-setup/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sunilksamanta%2Fnode-mongoose-setup/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/sunilksamanta","download_url":"https://codeload.github.com/sunilksamanta/node-mongoose-setup/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":250251594,"owners_count":21399838,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","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":["express","hacktoberfest2020","mongodb","mongoose","nodejs","rest-api"],"created_at":"2024-11-16T22:31:21.220Z","updated_at":"2025-04-22T13:47:44.867Z","avatar_url":"https://github.com/sunilksamanta.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"![REST API Setup Nodejs MongoDB](https://dev-to-uploads.s3.amazonaws.com/i/zxvx4pk4tdlqyk1x2a4c.png)\n\n![example workflow name](https://github.com/sunilksamanta/node-mongoose-setup/workflows/Node.js%20CI/badge.svg)\n[![Build Status](https://travis-ci.org/sunilksamanta/node-mongoose-setup.svg?branch=master)](https://travis-ci.org/sunilksamanta/node-mongoose-setup)\n[![Maintainability](https://api.codeclimate.com/v1/badges/25d8ccce7230ad5eb5c3/maintainability)](https://codeclimate.com/github/sunilksamanta/node-mongoose-setup/maintainability)\n[![FOSSA Status](https://app.fossa.com/api/projects/git%2Bgithub.com%2Fsunilksamanta%2Fnode-mongoose-setup.svg?type=shield)](https://app.fossa.com/projects/git%2Bgithub.com%2Fsunilksamanta%2Fnode-mongoose-setup?ref=badge_shield)\n[![PRs Welcome](https://img.shields.io/badge/PRs-welcome-brightgreen.svg?style=flat-square)](contributing.md)\n[![GitHub issues](https://img.shields.io/github/issues/sunilksamanta/node-mongoose-setup)](https://github.com/sunilksamanta/node-mongoose-setup/issues)\n[![GitHub stars](https://img.shields.io/github/stars/sunilksamanta/node-mongoose-setup)](https://github.com/sunilksamanta/node-mongoose-setup/stargazers)\n[![Twitter](https://img.shields.io/twitter/url?style=flat-square\u0026url=https%3A%2F%2Fgithub.com%2Fsunilksamanta%2Fnode-mongoose-setup)](https://twitter.com/intent/tweet?text=Wow:\u0026url=https%3A%2F%2Fgithub.com%2Fsunilksamanta%2Fnode-mongoose-setup)\n## Installation\n\nSetup is super easy. Clone the repository - \n\n```shell script\ngit clone https://github.com/sunilksamanta/node-mongoose-setup\ncd node-mongoose-setup\nnpm install\n```\n\nCreate an ``.env`` file at the root of your project with the following.  \n\n\n```dotenv\nMONGO_URL=YOUR_MONGO_URL\nPORT=5000[YOUR_DESIRED_PORT]\nNODE_ENV=YOUR_APP_ENVIRONMENT[production/development]\nJWT_SECRET=YOUR_JWT_SECRET_STRING\n```\n\nAn example file `.env.example` is included.\n\nYour project is ready. Now start the project.\n\n```shell script\nnpm start\n```\n\nGo to ``http://localhost:5000``. You should see a default welcome page.\n\nYour API base path is ``http://localhost:5000/api``.\n\nFirst create some accounts to get started with the authentication.\n\n## Authentication\n\nJWT authentication is added in this project. User model is defined in models/User.js. \nFor Register, Login, Logout use these urls —\n```\n    [POST] api/auth/register\n    [POST] api/auth/login\n    [GET] api/auth/logout\n```\n\n## Features\n\n1. **Controller, Model \u0026 Service oriented architecture**\n\n1. **Auth with JWT \u0026 Db Store**\n\n1. **Async/Await support**\n\n1. **User Module**\n\n1. **Post Module** (Sample CRUD)\n\n1. **Media Upload**\n\n1. **Centralized Http Response**\n\n1. **Error Handler**\n\n1. **.env support**\n\n1. **Multi Environment config setup**\n\n1. **Autobind Methods**\n\n1. **Built in Pagination**\n\n## Directory Structure of the Project\n```\n├─ .env\n├─ .gitignore\n├─ config\n│  ├─ config.js\n│  ├─ database.js\n│  ├─ routes.js\n│  └─ server.js\n├─ index.js\n├─ package.json\n├─ system\n└─ src\n  ├─ controllers\n  │  ├─ AuthController.js\n  │  ├─ MediaController.js\n  │  └─ PostController.js\n  ├─ helpers\n  ├─ models\n  │  ├─ Auth.js\n  │  ├─ Media.js\n  │  ├─ Post.js\n  │  └─ User.js\n  ├─ routes\n  │  ├─ auth.js\n  │  ├─ media.js\n  │  └─ post.js\n  └─ services\n     ├─ AuthService.js\n     ├─ MediaService.js\n     ├─ PostService.js\n     └─ UserService.js\n```\n## Lets talk about the structure\n\nWe have 2 base classes — One for Controller and another for Service.\n\n1. **[Controller.js](system/controllers/Controller.js)** \n\nThis base controller have the basic CRUD operations. To create a new controller just extend this base Controller class.\n\n2. **[Service.js](system/services/Service.js)**\n \nThis is the base Service class which includes the database operations. If you want to change the default behaviour of the services you can update this file.\n\n\n## How to Create new CRUD Module?\n\nIf you want to create a new Module say Post. Then you’ll have to create 4 basic files. One Controller, one Service, one Model and one route file. And add the new route in routes/index.js with desired url.\nThere is a “Post” CRUD module included in this project for example.\n```   \n    controllers/PostController.js\n    models/Post.js\n    services/PostService.js\n    routes/post.js\n```\n## Overriding Base class method\n\nAs an example if you see in the media Controller — the default delete method is overriden by its own class method as we have to delete the file from the file system also. So the overriden method is like bellow —\n```js\nasync delete(req, res, next) {\n  const { id } = req.params;\n  try {\n      const response = await this.service.delete(id);\n      // File Unlinking..\n      if (response.data.path) {\n          console.log(\"unlink item\", response.data.path);\n          fs.unlink(response.data.path, function (err) {\n              if (err) {\n                  console.log(\"error deleting file\");\n                  throw err;\n              }\n              console.log(\"File deleted!\");\n          });\n      }\n      return res.status(response.statusCode).json(response);\n  }\n  catch (e) {\n      next(e);\n  }\n}\n```\n\n## Have questions or suggestions?\nYou can reply to this article [REST API Structure using NodeJS MongoDB (Mongoose)](https://medium.com/@sunilksamanta/rest-api-architecture-using-nodejs-mongodb-mongoose-64f9021c906f)\n\n## Want to contribute?\n\nIf you have any suggestion, feedback or you want to make this project more powerful — feel free to report issues or request a feature or suggest some changes.\n\nRead the [Contributing guideline](contributing.md).\n\n## License\n\nThis project is licensed under the terms of the [MIT](LICENSE) license.\n\n\n\n[![FOSSA Status](https://app.fossa.com/api/projects/git%2Bgithub.com%2Fsunilksamanta%2Fnode-mongoose-setup.svg?type=large)](https://app.fossa.com/projects/git%2Bgithub.com%2Fsunilksamanta%2Fnode-mongoose-setup?ref=badge_large)\n\n## Credit\n\nSpecial thanks to [@thisk8brd](https://github.com/thisk8brd/node-starter) for the concept of this API Structure.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsunilksamanta%2Fnode-mongoose-setup","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fsunilksamanta%2Fnode-mongoose-setup","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsunilksamanta%2Fnode-mongoose-setup/lists"}