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 🙏

© 2026 – Pkg Stats / Ryan Hefner

gen-backend

v2.0.3

Published

A CLI tool to generate a rock solid backend setup using MongoDB, Express and Node

Readme

Backend Project Generator

This is a CLI tool to quickly generate a backend project with predefined templates and dependencies. It sets up a basic Node.js and Express server, with MongoDB support, and common utilities like CORS, and dotenv. This tool is ideal for developers who want to quickly scaffold out backend applications with best practices already in place.

Features

  • Automatic project initialization: Run a simple command to scaffold a backend project.
  • Predefined structure: Automatically creates directories config, controllers, models, middlewares, routes, services and utils.
  • Template files: Copies common backend files like server.js, db.js, app.js, etc.
  • Dependencies: Installs commonly used packages express, mongoose, cors, dotenv and morgan.
  • Configuration setup: Asks for basic configuration like port number, MongoDB URL, and database name.
  • Dev Setup: Installs nodemon for auto-restarting during development.

Prerequisites

Before using this tool, make sure you have the following installed:

Installation

Option 1: As a global command (for easy use anywhere) To install the tool globally on your system, run the following command:

  npm install -g gen-backend

Once installed, you can create a new backend project by running:

gen-backend --yes

This will initialize a project in the current directory and automatically set up everything for you.

Option 2: As a local project generator To use the generator within a specific project, you can run the following:

  1. Clone the repository:
https://github.com/ashikurrafi/Gen-Backend.git
  1. change directory
cd gen-backend
  1. Install the dependencies:
npm install
  1. Run the generator:
node index.js --yes

The tool will automatically:

  • Initialize the project (npm init --yes).
  • Install the necessary dependencies.
  • Create the project structure.
  • Copy predefined template files.
  • Update the package.json file.
  • Set up the .env configuration.

Usage

After setting up the project, you can start the server by running the following command:

npm run dev

This will start the server using nodemon, which watches for changes and restarts the server automatically.

Configuration

During the setup process, the tool will ask you for the following inputs:

  • Port Number: The port on which your server will run (default: 8000).
  • Database Name: The name of your MongoDB database (default: My_DB).
  • MongoDB URL: The MongoDB connection URL (default: mongodb://0.0.0.0:27017).

These values will be stored in a .env file for use in your application.

File Structure

After running the generator, your project will have the following structure:

📦 Your project directory
    +---src
        +---config
            +---db.js
        +---controllers
        +---error
            +---apiError.js
            +---apiResponse.js
            +---apiHandler.js
        +---middlewares
        +---models
        +---routes
            +---api.js
            +---index.js
        +---services
        +---utils
        +---app.js
        +---server.js

API Route (/api/v1/demo)

Once the project is generated, it will include a simple API route (http://localhost:<PORT>/api/v1/demo) that returns a success message:

GET /api/v1/demo

Response:

{
  "message": "API is working"
}

apiError usage

  1. Throwing an API error:
import apiError from "./apiError.js";

// Example: Throw a 400 Bad Request error with custom message
throw new apiError(400, "Invalid input data", [
  { field: "email", error: "Invalid format" },
]);
  1. Catching the error in a centralized error handler: In your main app, use middleware to catch the error and send an appropriate response
import express from "express";
const app = express();

// Centralized error handler
app.use((err, req, res, next) => {
  if (err instanceof apiError) {
    return res.status(err.statusCode).json({
      success: false,
      message: err.errorMessage,
      errors: err.errors,
    });
  }
  // Handle other errors here
  res.status(500).json({
    success: false,
    message: "Internal server error",
  });
});

apiResponse usage

  1. Sending a successful response:
import apiResponse from "./apiResponse.js";

// Example: Sending a successful response with data
const response = new apiResponse(
  200,
  { userId: 123, name: "John Doe" },
  "User fetched successfully"
);
res.status(response.statusCode).json({
  success: response.success,
  message: response.message,
  data: response.data,
});
  1. For success responses in controllers:
// Example controller function
const getUser = (req, res) => {
  const user = getUserFromDb(req.params.userId);
  const response = new apiResponse(200, user, "User data retrieved");
  res.status(response.statusCode).json(response);
};

asyncHandler usage

Wrap your async route handler with asyncHandler:

import asyncHandler from "./asyncHandler.js";

// Example of using asyncHandler in a route
const getUser = asyncHandler(async (req, res, next) => {
  const user = await getUserFromDb(req.params.userId);
  if (!user) {
    throw new apiError(404, "User not found");
  }
  const response = new apiResponse(200, user);
  res.status(response.statusCode).json(response);
});

app.get("/user/:userId", getUser);

Dependencies

This project uses the following dependencies:

  • express: Fast, unopinionated, minimalist web framework for Node.js.
  • mongoose: MongoDB object modeling for Node.js.
  • cors: Provides a middleware for enabling Cross-Origin Resource Sharing (CORS).
  • dotenv: Loads environment variables from a .env file.
  • morgan: HTTP request logger middleware.
  • nodemon: Development tool to automatically restart the server on code changes.

Contributing

Feel free to fork the repository and submit pull requests with improvements or bug fixes. If you encounter any issues, please open an issue, and we’ll try to address it as soon as possible.

How to contribute:

  1. Fork the repository.
  2. Create a new branch for your changes (git checkout -b your-feature).
  3. Commit your changes.
  4. Push to your forked repository.
  5. Create a pull request.

License

This project is open-source and available under the MIT License.

Acknowledgements

Inspired by various Node.js starter templates. Thanks to the contributors for improvements and feedback!