Giter Site home page Giter Site logo

dan-lucian / server-node-mongo-boilerplate Goto Github PK

View Code? Open in Web Editor NEW
0.0 1.0 0.0 328 KB

A basic boilerplate for a backend server built with Node.js using MongoDB as the database.

License: MIT License

JavaScript 99.58% HTML 0.42%
boilerplate express jest jwt mongodb-atlas rest-api

server-node-mongo-boilerplate's Introduction

RESTful express + mongo server boilerplate

A server boilerplate built upon express + mongoDB. The boilerplate comes with an authentication system based on jwt and refresh tokens fully covered by tests.

Table of contents

Install

First step, copy the repository and run:

npm i

Second step, you have to setup environment variables. Create a .env file at the top path of the repo and add the following variables each on a new line:

  • PORT= server port during develompent
  • MONGODB_URI= atlas uri to connect to
  • TEST_MONGODB_URI= atlas uri to connect to during tests
  • SECRET= secret key for the jsonwebtoken
  • TEST_SECRET= secret key for the jsonwebtoken duting tests

Third step, run the server in development mode which will be aided by nodemon:

npm run dev

Features

Auth

Authentication and authorization are built on a jwt + refresh tokens and roles such as "user" and "admin". Refresh tokens expire in a week and their purpose is to periodically get jwt tokens which expire in 15 min. The refresh token is sent in a http only cookie while the jwt token inside the "Authorization" header.

The bulk of authorization is done by the /middleware/authorize.js middleware, which can be attached on any route.

API

Authentication routes:
POST /accounts/register
POST /accounts/verify-email
POST /accounts/authenticate
POST /accounts/refresh-token - refresh the jwt token
POST /accounts/revoke-token - revoke the refresh token
POST /accounts/forgot-password
POST /accounts/validate-reset-token
POST /accounts/reset-password
POST /accounts - create an account

GET /accounts - get all accounts GET /accounts/:id

PUT /accounts/:id

DELETE /accounts/:id

Tests

Currently there are integration tests for authentication and authorization. Run the tests with:

npm run test
// or
npm test -- tests/integration/accounts.test.js

Folder structure

src\
 |--build\                         # The app to serve on the frontend
 |--config\                        # Env variables and configuration 
 |--features\                      # Feature based modules
    |-- name                       # A certain feature
        |-- name.controller.js     # A feature's controller
        |-- name.model.js          # A feature's model (may be more)
        |-- name.service.js        # A feature's business logic
 |--middleware\                    # Custom express middlewares
 |--utils\                         # Utility classes and functions
 |--app.js                         # Express app
 |--index.js                       # App entry point

Inspirations

  1. Great auth node + mongo boilerplate
  2. Great overall node + mongo boilerplate which also has TESTS!!!

Final words

Why use it? Right. Don't use it.

The resources provided above are way, way better. This is a custom boilerplate made to my needs.

server-node-mongo-boilerplate's People

Contributors

dan-lucian avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.