1.0.14 • Published 1 year ago

express-setup-cli-tridi v1.0.14

Weekly downloads
-
License
MIT
Repository
-
Last release
1 year ago

Express Setup CLI

šŸš€ Express Setup CLI is a command-line tool to quickly scaffold an Express.js project with essential features like authentication, middleware, database connection, and file uploads.

Features

āœ… Auto-generates an Express.js project structure āœ… Installs required dependencies āœ… Configures MongoDB connection āœ… Supports optional Cloudinary CDN setup āœ… Supports optional Multer file upload setup āœ… Creates .env file with placeholders āœ… Sets up authentication middleware āœ… Works with a single command like npx create-react-app


Installation & Usage

1ļøāƒ£ Install Globally

npm install -g express-setup-cli

Then run:

express-setup my-express-app

2ļøāƒ£ Use Directly with NPX

npx express-setup my-express-app

This will:

  • Create a new Express.js project in the my-express-app folder
  • Install all required dependencies
  • Ask if you want to use Cloudinary for CDN
  • Ask if you want to use Multer for file uploads
  • Generate project files & folder structure

Folder Structure

my-express-app/
│── src/
│   ā”œā”€ā”€ controllers/
│   ā”œā”€ā”€ db/
│   ā”œā”€ā”€ middlewares/
│   ā”œā”€ā”€ models/
│   ā”œā”€ā”€ routes/
│   ā”œā”€ā”€ utils/
│── public/
│── .env
│── app.js
│── index.js
│── constant.js
│── package.json
│── .gitignore

Environment Variables (.env)

The CLI generates a .env file with placeholders:

PORT=8000
CORS_ORIGIN=*
MONGODB_URI=your_mongodb_uri
ACCESS_TOKEN_SECRET=your_secret
REFRESH_TOKEN_SECRET=your_secret
ACCESS_TOKEN_EXPIRY=1d
REFRESH_TOKEN_EXPIRY=15d
CLOUDINARY_API_KEY=your_key
CLOUDINARY_CLOUD_NAME=your_cloud_name
CLOUDINARY_API_SECRET=your_secret

Cloudinary Integration (Optional)

If you choose Cloudinary during setup, a cloudinary.js file is created in src/utils/ with the following code:

import { v2 as cloudinary } from 'cloudinary';
import fs from "fs";

cloudinary.config({
    cloud_name: process.env.CLOUDINARY_CLOUD_NAME,
    api_key: process.env.CLOUDINARY_API_KEY,
    api_secret: process.env.CLOUDINARY_API_SECRET
});

const uploadCloudinary = async (localFilePath) => {
    try {
        if (!localFilePath) return null;
        const response = await cloudinary.uploader.upload(localFilePath, {
            resource_type: "auto",
        });
        console.log(response.url);
        fs.unlinkSync(localFilePath);
        return response;
    } catch (error) {
        fs.unlinkSync(localFilePath);
        return null;
    }
};

export { uploadCloudinary };

Multer Integration (Optional)

If you choose Multer during setup, a multer.middleware.js file is created in src/middlewares/ with:

import multer from "multer";

const storage = multer.diskStorage({
    destination: function (req, file, cb) {
        cb(null, "./public/temp");
    },
    filename: function (req, file, cb) {
        cb(null, file.originalname);
    }
});

export const upload = multer({ storage });

Authentication Middleware

This CLI also generates an authentication middleware auth.middleware.js:

import { ApiError } from "../utils/apiError.js";
import { asyncHandler } from "../utils/asyncHandler.js";
import jwt from "jsonwebtoken";
import { User } from "../models/user.model.js";

export const verifyJWT = asyncHandler(async (req, _, next) => {
    try {
        const token = req.cookies?.accessToken || req.header("Authorization")?.replace('Bearer ', "");
        if (!token) throw new ApiError(401, "Unauthorized Access");

        const decodeToken = jwt.verify(token, process.env.ACCESS_TOKEN_SECRET);
        const user = await User.findById(decodeToken?._id).select("-password -refreshToken");

        if (!user) throw new ApiError(401, "Invalid AccessToken");

        req.user = user;
        next();
    } catch (error) {
        throw new ApiError(401, error || "Invalid Access Token");
    }
});

Troubleshooting

If you run into issues: 1. Ensure dependencies are installed:

npm install -g express-setup-cli
  1. Check Node.js version (should be >= 18.x):
    node -v
  2. Make sure bin is correctly set in package.json:
    "bin": { "express-setup": "./bin/index.js" }
  3. If using NPX, clear cache and try again:
    npx clear-npx-cache

License

This project is MIT Licensed. Feel free to contribute and enhance it! šŸŽ‰


Contributors

šŸš€ Tridibesh Sarkar | GitHub | LinkedIn

1.0.14

1 year ago

1.0.13

1 year ago

1.0.12

1 year ago

1.0.11

1 year ago

1.0.10

1 year ago

1.0.9

1 year ago

1.0.8

1 year ago

1.0.7

1 year ago

1.0.6

1 year ago

1.0.5

1 year ago

1.0.4

1 year ago

1.0.3

1 year ago

1.0.2

1 year ago

1.0.0

1 year ago