npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

create-myexpress-api

v1.0.1

Published

Initial Boilerplate with Authentication + CRUD Ready at your Service.

Downloads

2

Readme

Express REST API Boilerplate

A CLI to automatically clone express-rest-api-boilerplate

Author

Installation

Install the CLI globally OR use npx:

  npm install -g create-myexpress-api

Usage

  # with global install
  create-myexpress-api name-of-your-app
  # with npx
  npx create-myexpress-api name-of-your-app

This will create a directory with the given name, clone the express API boilerplate repo into it, and install dependencies.

Features

  • ✔ Authentication Routes
    • ✔ Register
    • ✔ Login
    • ✔ Logout
    • ✔ Refresh Token
  • ✔ MongoDB Config Setup
  • ✔ Multer config setup for file upload at server
  • ✔ CRUD Product Routes
    • ✔ Unprotected

    • ✔ JWT Protected

      # Create,Delete,Update(protected)     
      role : admin
      # Get Single or all Products(unprotected)
      role: Customer

Environment Variables

To run this project, you will need to add the following environment variables to your .env file

API_PORT

DEBUG_MODE

DB_URL

JWT_SECRET

REFRESH_SECRET

IMAGE_DATABASE_DOMAIN_URL

API Reference

Authentication Routes

Register new user

  POST /api/register

Request Body

| Parameter | Type | Description | | :-------- | :------- | :-------------------------------- | | name | string | Required. Name of User | | email | string | Required. Email of User | | password | string | Required. Password for user | | repeat_password | string | Required. Confirm password | | role | string | Not Required. Default:Customer To be able to Create,Update,Delete Products set this to 'admin' |

Login user (returns access token validity 1 minute)

  POST /api/login

Request Body

| Parameter | Type | Description | | :-------- | :------- | :-------------------------------- | | email | string | Required. Email of User | | password | string | Required. Password for user |

200 ok Response

{

"access_token": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJfaWQiOiI2MGM1ZWRkNTVkZTMwYjM2OTA3YjQwMjkiLCJyb2xlIjoiYWRtaW4iLCJpYXQiOjE2MjQxMDc3MzUsImV4cCI6MTYyNDEwNzc5NX0.qpFlryiMUmbwPbxRjTDMkCZVLYL-yDi2it8vAF9hueU",
"refresh_token": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJfaWQiOiI2MGM1ZWRkNTVkZTMwYjM2OTA3YjQwMjkiLCJyb2xlIjoiYWRtaW4iLCJpYXQiOjE2MjQxMDc3MzUsImV4cCI6MTYyNDE5NDEzNX0.QXlXOppYhH1WyccN__wXdBYp71x8SIGrSL7gAgvyV3o"

}

Get user info

  GET /api/whoami

Headers

| Parameter | Type | Description | | :-------- | :------- | :-------------------------------- | | Authorization Bearer | string | Required. Access Token |

Use access token for making whoami,logout,refresh & CRUD requests

Get refresh token(1 day validity)

  POST /api/refresh

Request Body

| Parameter | Type | Description | | :-------- | :------- | :-------------------------------- | | refresh_token | string | Required. Refresh Token |

Use access token for making whoami,logout,refresh & CRUD requests

Logout user

  POST /api/logout

Request Body

| Parameter | Type | Description | | :-------- | :------- | :-------------------------------- | | refresh_token | string | Required. Refresh Token |

Headers

| Parameter | Type | Description | | :-------- | :------- | :-------------------------------- | | Authorization Bearer | string | Required. Access Token |

Use access token for making whoami,logout,refresh & CRUD requests

CRUD Protected(JWT) & Unprotected Routes

Get all products

  GET /api/products

Request Body

| Parameter | Type | Description | | :-------- | :------- | :------------------------- | | NA | NA | Not Applicable |

Get single product

  GET /api/products/${id}

Request Body

| Parameter | Type | Description | | :-------- | :------- | :-------------------------------- | | id | string | Required. Id of product to fetch |

Create new product

  POST /api/products/${id}

Request Body

| Parameter | Type | Description | | :-------- | :------- | :-------------------------------- | | name | string | Required. Name of product | | price | int | Required. Price of product | | quantity | int | Required. Quantity of product | | image | file | Required. Image of product |

Headers

| Parameter | Type | Description | | :-------- | :------- | :-------------------------------- | | Authorization Bearer | string | Required. Access Token |

Update existing product

  PUT /api/products/${id}

Request Body

| Parameter | Type | Description | | :-------- | :------- | :-------------------------------- | | id | string | Required. Id of product to be updated | | name | string | Required. Name of product | | price | int | Required. Price of product | | quantity | int | Required. Quantity of product | | image | file | Not Required. Image of product |

Headers

| Parameter | Type | Description | | :-------- | :------- | :-------------------------------- | | Authorization Bearer | string | Required. Access Token |

Delete existing product

  DELETE /api/products/${id}

Request Body

| Parameter | Type | Description | | :-------- | :------- | :-------------------------------- | | id | string | Required. Id of product to be deleted |

Headers

| Parameter | Type | Description | | :-------- | :------- | :-------------------------------- | | Authorization Bearer | string | Required. Access Token |

Appendix

API folder structure

    -express_rest_starter_kit
      *api
         *config
           -index.js
         *controllers
           -index.js
           -productController.js
           *auth
             -registerController.js
             -loginController.js
             -refreshController.js
             -whoamiController.js
         *middlewares
           -admin.js
           -auth.js
           -errorHandler.js
         *models
           -index.js
           -user.js
           -product.js
           -refreshToken.js
         *routes
           -index.js
         *services
           - CustomErrorHandler.js
           - JWTService.js
         *uploads
           - 3748374834-3293829.jpg
         *validators
           -productValidation.js
        -server.js

REST EXPRESS API Boilerplate Structure Information (WHAT IS WHAT ?)

  • [x] server.js the entry file to start your api

  • [x] config has (.env variables) exported via index.js

  • [x] controllers has all the routes logic which is exported via a common index.js

  • [x] middlewares contain auth.js (checking authorization header in request header) and errorHandler that act as general error handler for the entire api.

  • [x] models has the DB Schemas exported by a single file named index.js

  • [x] routes has all the routes path along with controller reference that is exported via common index.js which is then imported in the server.js

  • [x] services includes Class based modules like CustomErrorHandler for handling d/f error cases and JWTService for signing JWT token

  • [x] uploads folder contain all the image uploaded via the Create New Product Route

  • [x] validators contains Client Side Validation modules