Skip to content

waseem-mansha-gondal/nodejs-express-js-stater

Repository files navigation

RESTful API using Nodejs, Express with relational database Postgres, MySQL

Installation

git clone https://github.com/Global-Software-Consulting/super-express-app.git
cd super-express-app

Install the dependencies:

npm install

Set the environment variables:

cp .env.example .env

# open .env and modify the environment variables

Table of Contents

Features

  • postgres database: object data modeling using sequelize
  • Authentication: using jwt
  • Validation: validation of fields enabled using express-validator
  • API testing: with postman
  • Environment variables: using [dotenv]
  • CORS: Cross-Origin Resource-Sharing enabled using cors
  • Linting: with ESLint and Prettier
  • Editor config: consistent editor configuration

Commands

Running locally:

npm run dev

Running in production:

npm start

Migrations:

# run all migrations
npm run db:migrate

# undo migration
npm run db:migrate:undo

Seeders:

# run all seeders
npm run db:seed

# undo seeding
npm run db:seed:undo

Linting:

# run ESLint
npm run lint

Test:

# run tests
npm run test

Environment Variables

The environment variables can be found and modified in the .env file. They come with these default values:

# Port number
PORT=4000
#Database name
DB_NAME=here your db name
#Username 
DB_USERNAME=db user name
#Username set for postgres
DB_PASSWORD=database password
#locally you can use 'localhost'
DB_HOST=database host
# JWT secret key
JWT_SECRET=thisisasamplesecret
# Number of minutes after which an access token expires
JWT_EXPIRES_IN=30

Project Structure

src\
 |--config\         # Environment variables and configuration related things
 |--controllers\    # Route controllers
 |--middleware\     # Custom express middleware
 |--migrations\     # Database migrations
 |--models\         # Sequelize models
 |--routes\         # Routes
 |--seeders\        # Database seeders
 |--utils\          # Utility classes and functions
 |--validators\     # Validate fields
 |--index.js        # App entry point
test\
 

Stripe Integration

Create subscription Plans by admin

   "name":"basic",
    "monthlyPrice":5,
    "annualPrice":55

User can buy subscription monthly or yearly

  "interval": "monthly",
  "subscriptionPlanId": selected subscription plan id

Add payment card

    "number":payment card number,
    "exp_month":expiry month,
    "exp_year":expiry year

API Endpoints

List of available routes:

Blog routes:
POST /api/v1/blog - add blog
GET /api/v1/blog - get all blogs
GET /api/v1/blog/:id - get a blog
PATCH /api/v1/blog/:id - update blog
DELETE /api/v1/blog/:id - delete user

Authentication

To require authentication for certain routes, you can use the auth middleware.

const express = require('express');
const auth = require('../middleware/auth');
const { userController } = require('../controller');

const router = express.Router();

router.patch('/:id', auth, userController.update);

validation

using express-validator validate required fields

const express = require('express');
const auth = require('../middleware/auth');
const { userController } = require('../controller');
const { userValidator } = require('../validators');

const router = express.Router();

router.patch(
  '/signup',
  userValidator.signup(),
  userValidator.validate,
  auth,
  userController.signup
);

roles

Role base tasks

const { blogController } = require('../controllers');
const auth = require('../middleware/auth');
const { checkRole } = require('../middleware/roles');

router.post('/', auth, checkRole('admin', 'editor'), blogController.add);

Linting

Linting is done using ESLint and Prettier.

Heroku

Deployed on heroku here is live heroku link SuperApp

About

No description or website provided.

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published