raman-utils
v1.0.1
Published
Reusable utilities for my MERN projects
Readme
Perfect! Here’s a complete README.md for your raman-utils npm package. You can copy this into your project before publishing.
raman-utils
Reusable utilities for MERN projects (MongoDB, Express, JWT).
This package provides common functions and middleware like MongoDB connection, Express server setup, and JWT authentication for fast MERN development.
Installation
npm install raman-utils
Features
MongoDB connection utility (connectDB)
Express server setup (createServer)
JWT authentication middleware (authenticate)
Ready-to-use boilerplate for any MERN project
Usage
1. Importing
const { connectDB, authenticate, createServer } = require("raman-utils");
2. Connect to MongoDB
connectDB(process.env.MONGO_URI); // Connects to your MongoDB
3. Create an Express server
const app = createServer();
app.get("/", (req, res) => {
res.send("🚀 Server is running!");
});
app.listen(5000, () => console.log("Server running on port 5000"));
4. Protect Routes with JWT
app.get("/dashboard", authenticate, (req, res) => {
// req.userId is available after authentication
res.json({ message: "Welcome to the protected route!" });
});
Notes:
authenticate middleware checks:
Authorization header: Bearer <token>
Cookie: token
It attaches req.userId from the decoded JWT.
Functions
connectDB(url)
Connects to MongoDB.
Parameters:
Name Type Description
url string MongoDB connection string
createServer()
Creates a new Express server with JSON parsing middleware.
Returns: Express app instance.
authenticate(req, res, next)
Express middleware to protect routes.
Checks:
Authorization header (Bearer <token>)
Cookie (token)
Attaches:
req.userId → ID from JWT payload
Usage:
app.get("/protected", authenticate, (req, res) => {
res.send("You are authenticated!");
});
Environment Variables
Make sure to set these in your project:
MONGO_URI=your_mongodb_connection_string
JWT_SECRET=your_jwt_secret
Example Full Server
require("dotenv").config();
const { connectDB, authenticate, createServer } = require("raman-utils");
connectDB(process.env.MONGO_URI);
const app = createServer();
app.get("/public", (req, res) => res.send("Public route"));
app.get("/private", authenticate, (req, res) => {
res.json({ userId: req.userId, message: "Private route access granted" });
});
app.listen(5000, () => console.log("Server running on 5000"));
License
MIT © Raman Chauhan
Repository
https://github.com/yourusername/raman-utils
---
This README covers:
- Installation
- Usage examples
- Function details
- Environment variables
- Full server example
It’s ready to be **displayed automatically** on npm after you publish.
If you want, I can also **improve it with a table of all functions and parameters** for future functions you might add to `raman-utils`. This makes it look like professional documentation.
Do you want me to do that?