All Projects β†’ sunilksamanta β†’ node-mongoose-setup

sunilksamanta / node-mongoose-setup

Licence: MIT license
Nodejs MongoDB REST API Sarter

Programming Languages

javascript
184084 projects - #8 most used programming language
shell
77523 projects

Projects that are alternatives of or similar to node-mongoose-setup

Pharmacy-Mangment-System
πŸ‘¨β€πŸ’» πŸ₯ MEAN stack Pharmacy Management system.
Stars: ✭ 229 (+377.08%)
Mutual labels:  mongoose, hacktoberfest2020
OpenBook-E-Commerce
An e-commerce progressive web application, built with mern stack. It has features like product buy, order management by admin, payment gateway, cart, checkout and lot more.
Stars: ✭ 53 (+10.42%)
Mutual labels:  mongoose
VSCodePopTheme
A port of Pop! Theme for VSCode
Stars: ✭ 33 (-31.25%)
Mutual labels:  hacktoberfest2020
SpoketoberfestResources
No description or website provided.
Stars: ✭ 16 (-66.67%)
Mutual labels:  hacktoberfest2020
Dimensionality-reduction-and-classification-on-Hyperspectral-Images-Using-Python
In this repository, You can find the files which implement dimensionality reduction on the hyperspectral image(Indian Pines) with classification.
Stars: ✭ 63 (+31.25%)
Mutual labels:  hacktoberfest2020
wp-boilerplate-plugin-with-vuejs
This is an example plugin for Wp plugin developer.
Stars: ✭ 26 (-45.83%)
Mutual labels:  hacktoberfest2020
Backend-NodeJS-Golang-Interview QA
A collection of Node JS and Golang Backend interview questions please feel free to fork and contribute to this repository
Stars: ✭ 122 (+154.17%)
Mutual labels:  mongoose
Resources
No description or website provided.
Stars: ✭ 25 (-47.92%)
Mutual labels:  hacktoberfest2020
hacktoberfest2020
This is a hacktoberfest repo with learning propose to make pull request(PR) and get contribute on opensource project
Stars: ✭ 13 (-72.92%)
Mutual labels:  hacktoberfest2020
algorithm-zoo
Implementations of algorithms from http://quantumalgorithmzoo.org/
Stars: ✭ 17 (-64.58%)
Mutual labels:  hacktoberfest2020
ThinkApp
Test your knowledge with any of the available topic this fun and free Champion Quiz Master App. Save your time and effort by saving your queries & its resolutions
Stars: ✭ 15 (-68.75%)
Mutual labels:  mongoose
amiws
Asterisk Management Interface (AMI) to Web-socket proxy
Stars: ✭ 60 (+25%)
Mutual labels:  mongoose
Hackerrank-Codes
Here are some of the solutions to HackerRank questions.
Stars: ✭ 63 (+31.25%)
Mutual labels:  hacktoberfest2020
first-pr-repo
A step by step guide to help people make their first Pull Request
Stars: ✭ 29 (-39.58%)
Mutual labels:  hacktoberfest2020
hacktoberfest-beginner
Make your first contribution for Hacktoberfest in the simplest way
Stars: ✭ 16 (-66.67%)
Mutual labels:  hacktoberfest2020
security bear
πŸ§ΈπŸ›‘οΈ Security Bear provides easy integration with the device like making it connect to home WiFi through an app and getting secure and unique user credentials of the device in order to troubleshoot. If you have ever wondered "Is there an open-source project to make my own smart home?", This is just part of one.
Stars: ✭ 22 (-54.17%)
Mutual labels:  hacktoberfest2020
kdtree-in-python
Source Code for K-d tree in Python series
Stars: ✭ 61 (+27.08%)
Mutual labels:  hacktoberfest2020
yelp-camp
A Node.js web application project from the Udemy course - The Web Developer Bootcamp by Colt Steele
Stars: ✭ 36 (-25%)
Mutual labels:  mongoose
node-snimay
Node.js Example: Express, Mongoose, Express-hbs, JWT
Stars: ✭ 13 (-72.92%)
Mutual labels:  mongoose
boss
React+express+sock.io+mongodb build a boss
Stars: ✭ 25 (-47.92%)
Mutual labels:  mongoose

REST API Setup Nodejs MongoDB

example workflow name Build Status Maintainability FOSSA Status PRs Welcome GitHub issues GitHub stars Twitter

Installation

Setup is super easy. Clone the repository -

git clone https://github.com/sunilksamanta/node-mongoose-setup
cd node-mongoose-setup
npm install

Create an .env file at the root of your project with the following.

MONGO_URL=YOUR_MONGO_URL
PORT=5000[YOUR_DESIRED_PORT]
NODE_ENV=YOUR_APP_ENVIRONMENT[production/development]
JWT_SECRET=YOUR_JWT_SECRET_STRING

An example file .env.example is included.

Your project is ready. Now start the project.

npm start

Go to http://localhost:5000. You should see a default welcome page.

Your API base path is http://localhost:5000/api.

First create some accounts to get started with the authentication.

Authentication

JWT authentication is added in this project. User model is defined in models/User.js. For Register, Login, Logout use these urls β€”

    [POST] api/auth/register
    [POST] api/auth/login
    [GET] api/auth/logout

Features

  1. Controller, Model & Service oriented architecture

  2. Auth with JWT & Db Store

  3. Async/Await support

  4. User Module

  5. Post Module (Sample CRUD)

  6. Media Upload

  7. Centralized Http Response

  8. Error Handler

  9. .env support

  10. Multi Environment config setup

  11. Autobind Methods

  12. Built in Pagination

Directory Structure of the Project

β”œβ”€ .env
β”œβ”€ .gitignore
β”œβ”€ config
β”‚  β”œβ”€ config.js
β”‚  β”œβ”€ database.js
β”‚  β”œβ”€ routes.js
β”‚  └─ server.js
β”œβ”€ index.js
β”œβ”€ package.json
β”œβ”€ system
└─ src
  β”œβ”€ controllers
  β”‚  β”œβ”€ AuthController.js
  β”‚  β”œβ”€ MediaController.js
  β”‚  └─ PostController.js
  β”œβ”€ helpers
  β”œβ”€ models
  β”‚  β”œβ”€ Auth.js
  β”‚  β”œβ”€ Media.js
  β”‚  β”œβ”€ Post.js
  β”‚  └─ User.js
  β”œβ”€ routes
  β”‚  β”œβ”€ auth.js
  β”‚  β”œβ”€ media.js
  β”‚  └─ post.js
  └─ services
     β”œβ”€ AuthService.js
     β”œβ”€ MediaService.js
     β”œβ”€ PostService.js
     └─ UserService.js

Lets talk about the structure

We have 2 base classes β€” One for Controller and another for Service.

  1. Controller.js

This base controller have the basic CRUD operations. To create a new controller just extend this base Controller class.

  1. Service.js

This 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.

How to Create new CRUD Module?

If 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. There is a β€œPost” CRUD module included in this project for example.

    controllers/PostController.js
    models/Post.js
    services/PostService.js
    routes/post.js

Overriding Base class method

As 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 β€”

async delete(req, res, next) {
  const { id } = req.params;
  try {
      const response = await this.service.delete(id);
      // File Unlinking..
      if (response.data.path) {
          console.log("unlink item", response.data.path);
          fs.unlink(response.data.path, function (err) {
              if (err) {
                  console.log("error deleting file");
                  throw err;
              }
              console.log("File deleted!");
          });
      }
      return res.status(response.statusCode).json(response);
  }
  catch (e) {
      next(e);
  }
}

Have questions or suggestions?

You can reply to this article REST API Structure using NodeJS MongoDB (Mongoose)

Want to contribute?

If 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.

Read the Contributing guideline.

License

This project is licensed under the terms of the MIT license.

FOSSA Status

Credit

Special thanks to @thisk8brd for the concept of this API Structure.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].