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 🙏

© 2025 – Pkg Stats / Ryan Hefner

node-api-maker

v1.1.0

Published

A CLI tool to generate REST API modules (model, controller, route, validator) with one command

Readme

📦 node-api-maker

npm downloads license

npm

A powerful CLI tool to generate REST API modules (Model, Controller, Route, Validator) with one command.


🚀 Features

  • 🔧 Generate full CRUD modules in seconds
  • 🧩 Supports field definition via --fields
  • 🧾 Supports reusable presets with --preset=<filename> option
  • 📁 Auto-generates folders: models/, controllers/, routes/, validators/
  • ✅ Includes validation using Joi
  • 🔒 Express-compatible and middleware-ready
  • ⚡ Works with npx, no global install needed
  • 📄 Auto-generates Swagger documentation (utils/swagger.js)

📥 Installation

You can use it directly via npx:

npx node-api-maker User --fields=name:string,email:string,isActive:boolean

Or install globally:

npm install -g node-api-maker

📘 Usage

You can generate REST API modules in two ways:

1️⃣ Using Inline Fields

Define fields directly in the command using the --fields option:

npx node-api-maker <ModelName> --fields=field:type,field:type

Supported types: string, number, boolean

💡 Example

npx node-api-maker Product --fields=name:string,price:number,inStock:boolean

2️⃣ Using a Preset File

For reusability and cleaner commands, you can define fields in a preset JSON file.

  1. Create a folder named presets/ in your project root.
  2. Add a file like product.json with the field definitions.

Example: presets/product.json

{
  "name": "string",
  "price": "number",
  "inStock": "boolean",
  "category": "string"
}

Then run:

npx node-api-maker Product --preset=product

This will generate the same module using the field definitions from presets/product.json.

📦 Loaded fields from preset: product
📄 Created utils/swagger.js for Swagger support
✅ Successfully generated REST API for "Product"
📁 Files created:
- models/product.model.js
- controllers/product.controller.js
- validators/product.validator.js
- routes/product.routes.js
📄 Created default app.js with Swagger and placeholders
🔌 Route successfully registered in app.js ✅
Free palestine 🙏 Love From 🇧🇩

📁 What Gets Generated?

Running the above command will create:

models/product.model.js
controllers/product.controller.js
validators/product.validator.js
routes/product.routes.js

It also auto-injects into:

app.js
utils/swagger.js  (created if missing)

🧪 Sample Output

✅ models/product.model.js

import mongoose from 'mongoose';

const productSchema = new mongoose.Schema({
  name: { type: String, required: true },
  price: { type: Number, required: true },
  inStock: { type: Boolean, required: true },
}, { timestamps: true });

export default mongoose.model('Product', productSchema);

✅ validators/product.validator.js

import Joi from 'joi';

export const productSchema = Joi.object({
  name: Joi.string().required(),
  price: Joi.number().required(),
  inStock: Joi.boolean().required(),
});

✅ controllers/product.controller.js

import Product from '../models/product.model.js';

export const getAllProducts = async (req, res) => {
  const data = await Product.find();
  res.json(data);
};

export const getProduct = async (req, res) => {
  const data = await Product.findById(req.params.id);
  if (!data) return res.status(404).json({ message: 'Product not found' });
  res.json(data);
};

export const createProduct = async (req, res) => {
  const data = await Product.create(req.body);
  res.status(201).json(data);
};

export const updateProduct = async (req, res) => {
  const data = await Product.findByIdAndUpdate(req.params.id, req.body, { new: true });
  res.json(data);
};

export const deleteProduct = async (req, res) => {
  await Product.findByIdAndDelete(req.params.id);
  res.json({ message: 'Product deleted' });
};

✅ routes/product.routes.js

import express from 'express';
import {
  getAllProducts, getProduct, createProduct, updateProduct, deleteProduct
} from '../controllers/product.controller.js';
import { verifyToken, isAdmin } from '../middlewares/auth.middleware.js';
import validate from '../middlewares/validate.middleware.js';
import { productSchema } from '../validators/product.validator.js';

const router = express.Router();

router.get('/', verifyToken, getAllProducts);
router.get('/:id', verifyToken, getProduct);
router.post('/', verifyToken, isAdmin, validate(productSchema), createProduct);
router.put('/:id', verifyToken, isAdmin, validate(productSchema), updateProduct);
router.delete('/:id', verifyToken, isAdmin, deleteProduct);

export default router;

🛡 Requirements

  • Node.js v14 or higher
  • An existing Express.js app with middleware setup
  • Joi for schema validation (already included as a dependency)

🙋‍♂️ Author

Md. Majharul Islam

Feel free to contribute, suggest improvements, or report issues.

📄 License

This project is licensed under the MIT License.