Skip to content

A Boilerplate for Node.js web applications backed by MongoDB following MVCS pattern.

License

Notifications You must be signed in to change notification settings

luisgcenci/nodejs-starter

Repository files navigation

NodeJS Backend App - Starter

NodeJS app on express framework backed by a MongoDB database following MVC pattern. Includes User Model with CRUD services, as well signin/signup endpoints. Passwords are encrypted using bcryptjs.

Minimum Dependencies

NodeJS
MongoDB
Yarn

Features

Authentication using Email and Password
Add (signup/signin) / Update / Delete Users

Application Structure

backend
├── config
│   └── database.js
├── controllers
│   └── User.controller.js
├── middleware
│   └── auth.js
├── models
│   └── User.js
├── routes
│   └── User.routes.js
├── server.js
└── services
    └── UserService.js
  • config/ - Mongoose config to connect with MongoDB.
  • controllers/ - Serves the responses.
  • middleware/ - Verifies JWT Token.
  • models/ - Schema definitions for mongoose models.
  • routes/ - Routes for API.
  • server.js - Entry point of application.
  • services/ - Business logic between controllers and models.

Set Up

Clone this repo

https://github.com/luisgcenci/signup-login-nodejs-backend-app.git

Install Dependencies

yarn install

Open .env file and insert mongodb uri and replace TOKEN_KEY with a random string.

TOKEN_KEY is used to create JWT Auth.

MONGO_URI="mongodb+srv://username:[email protected]/?retryWrites=true&w=majority"
TOKEN_KEY="RANDOMSTRING"

How to Run

node server.js

Endpoints

URL: /api/v1/user/signup

Method : POST

Auth required : NO

Data Params (Body)

{
    "username": "username",
    "password": "password"
}

Success Response

Code : 201 OK

Returns JWT Auth Token (application/json)

Content example

{
    "jwt_token": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VyX2lkIjoiNjJjZjliZThmM2MyYTM0NDdlMjEwMjc3IiwidXNlcm5hbWUiOiJyb290IiwiaWF0IjoxNjU3NzczMDMyLCJleHAiOjE2NTc3ODAyMzJ9.2avOPBKgWKcLYdmjs6z5j8Yr8rgi4GossoFyLC6pEg0"
}

URL: /api/v1/user/signin

Method : POST

Auth required : NO

Data Params (Body)

{
    "username": "username",
    "password": "password"
}

Success Response

Code : 201 OK

Returns JWT Auth Token (Application JSON)

Content example

{
    "jwt_token": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VyX2lkIjoiNjJjZjliZThmM2MyYTM0NDdlMjEwMjc3IiwidXNlcm5hbWUiOiJyb290IiwiaWF0IjoxNjU3NzczMDMyLCJleHAiOjE2NTc3ODAyMzJ9.2avOPBKgWKcLYdmjs6z5j8Yr8rgi4GossoFyLC6pEg0"
}

URL: /api/v1/user/updateusername

Method : POST

Auth required : NO

Data Params (Body)

{
    "username": "username",
    "password": "password",
    "newUsername": "newUsername"
}

Success Response

Code : 201 OK

Returns user object (application/json)

Content example

{
    "_id": "62cf9be8f3c2a3447e210277",
    "username": "newUsername",
    "password": "$2a$10$Z9XNnzvPA77VnggiEtE9juJvFNXFbVVMtSrhNJTPDfpKLM2jF/keO",
    "token": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VyX2lkIjoiNjJjZjliZThmM2MyYTM0NDdlMjEwMjc3IiwidXNlcm5hbWUiOiJyb290IiwiaWF0IjoxNjU3NzczMDMyLCJleHAiOjE2NTc3ODAyMzJ9.2avOPBKgWKcLYdmjs6z5j8Yr8rgi4GossoFyLC6pEg0",
    "__v": 0
}

URL: /api/v1/user/updatepassword

Method : POST

Auth required : NO

Data Params (Body)

{
    "username": "username",
    "password": "password",
    "newPassword": "newPassword"
}

Success Response

Code : 201 OK

Returns user object (Application JSON)

Content example

{
    "_id": "62cf9be8f3c2a3447e210277",
    "username": "username",
    "password": "newPasswordHashed",
    "token": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VyX2lkIjoiNjJjZjliZThmM2MyYTM0NDdlMjEwMjc3IiwidXNlcm5hbWUiOiJyb290IiwiaWF0IjoxNjU3NzczMDMyLCJleHAiOjE2NTc3ODAyMzJ9.2avOPBKgWKcLYdmjs6z5j8Yr8rgi4GossoFyLC6pEg0",
    "__v": 0
}

URL: /api/v1/user/deleteuser

Method : DELETE

Auth required : NO

Data Params (Body)

{
    "username": "username",
    "password": "password"
}

Success Response

Code : 201 OK

Returns the count of users deleted. (Application JSON)

Content example

{
    "deletedCount": 1
}

About

A Boilerplate for Node.js web applications backed by MongoDB following MVCS pattern.

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published