autonodeapi
v1.0.8
Published
For auto-generating APIs
Readme
🚀 autonodeapi Documentation
autonodeapi is a powerful Node.js package that automates the generation of CRUD APIs for Mongoose models. It also generates a Swagger documentation endpoint for easy API reference.
📌 Features
✅ Automatically generates CRUD APIs for Mongoose models
✅ Supports dynamic model creation with user-defined fields
✅ Generates a fully documented Swagger API at /api/docs
✅ Reduces boilerplate code, making development faster
✅ Easy integration with existing Express applications
📌 Installation
First, install the required dependencies using npm:
npm install autonodeapi
⚙️ Usage
Run the following command to generate your API:
npx generate-auth //to login, logout, signup functionality (once)
npx generate // when need to create api for a model run this command
1️⃣ Provide Model Name
After running the command, it will ask for a model name. Example:
Enter model name: user
(The model name should be in singular, and it will be created in the /models directory.)
2️⃣ Provide Model Fields
Next, enter the fields for your model in the format:
fieldName:DataType
Supported Data Types: String, Number, Boolean, Date, Array, Object
Example:
Enter fields (comma-separated): username:String, password:Number
This will create a User model with:
const mongoose = require("mongoose");
const UserSchema = new mongoose.Schema({
username: { type: String, required: true },
password: { type: Number, required: true }
});
module.exports = mongoose.model("User", UserSchema);
📌 What Happens Next?
✅ A Mongoose Model is created in /models
✅ A Controller is created in /controllers
✅ A Route is created in /routes
✅ A Swagger documentation file is updated
📌 Register Routes
You can skip this next steps if you used the command - npx generate-auth
To make the generated API work, register the routes in index.js or server.js.
1️⃣ Import Routes
import routes from "./routes/index.js";
import swaggerRoutes from "./swagger.js";
2️⃣ Use Routes in Express App
app.use("/api", routes);
app.use("/api", swaggerRoutes);
📌 View API Documentation
Once everything is set up, you can view your API documentation at:
http://localhost:<PORT>/api/docs
(Replace <PORT> with your actual port number, e.g., 3000.)
This will show a list of all generated API endpoints.
✅ Generated Endpoints
For a model named User, the following CRUD endpoints will be available:
| Method | Endpoint | Description |
| --- | --- | --- |
| POST | /api/user | Create a new user |
| GET | /api/user | Get all users |
| GET | /api/user/:id | Get user by ID |
| PUT | /api/user/:id | Update user by ID |
| DELETE | /api/user/:id | Delete user by ID |
🎯 Example API Request
To create a new user, send a POST request to:
POST http://localhost:3000/api/user
With a JSON body:
{
"username": "john_doe",
"password": 123456
}
🎯 Example API Response
A successful response will return:
{
"success": true,
"data": {
"_id": "65fa1b2e3d5f2b001fdd1234",
"username": "john_doe",
"password": 123456,
"createdAt": "2025-03-14T12:00:00.000Z"
}
}
🔗 Registering Routes
To use the generated routes, add them to your index.js or server.js file:
import express from "express";
import routes from "./routes/index.js";
import swaggerRoutes from "./swagger.js";
const app = express();
app.use(express.json()); // Middleware for JSON parsing
// Register API routes
app.use("/api", routes);
// Register Swagger documentation route
app.use("/api", swaggerRoutes);
const PORT = process.env.PORT || 5000;
app.listen(PORT, () => {
console.log(`🚀 Server running on http://localhost:${PORT}`);
});
📜 Viewing API Documentation
Once the server is running, open your browser and visit:
http://localhost:<PORT>/api/docs
This will display a Swagger UI showing all available API endpoints.
🎯 Example Swagger UI:
/api/users(GET) → Fetch all users/api/users/:id(GET) → Fetch a user by ID/api/users(POST) → Create a new user/api/users/:id(PUT) → Update an existing user/api/users/:id(DELETE) → Delete a user
🛠️ Customization & Extensions
Since autonodeapi generates standard Express routes, controllers, and models, you can customize them as needed.
For example, to add authentication, middleware, or custom business logic, edit the generated controller files inside the controllers/ directory.
🎯 Why Use autonodeapi?
✅ Saves time by auto-generating boilerplate CRUD APIs
✅ Easy to set up and integrate with existing Express projects
✅ Built-in Swagger documentation for better API management
✅ Fully customizable and scalable
🚀 Start Building APIs in Seconds!
This tool saves hours of work by automatically generating:
✅ Models
✅ Routes
✅ Controllers
✅ Swagger API documentation
Just run:
npx generate-auth
npx generate
Provide a model name and fields, and you're ready to go! 🎉🔥
