express-setup-cli-tridi v1.0.14
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-cliThen run:
express-setup my-express-app2ļøā£ Use Directly with NPX
npx express-setup my-express-appThis will:
- Create a new Express.js project in the
my-express-appfolder - 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
āāā .gitignoreEnvironment 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_secretCloudinary 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- Check Node.js version (should be >= 18.x):
node -v - Make sure
binis correctly set inpackage.json:"bin": { "express-setup": "./bin/index.js" } - 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! š