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 🙏

© 2024 – Pkg Stats / Ryan Hefner

generate-node-module

v1.0.1

Published

A CLI tool to generate module files with basic templates.

Downloads

6

Readme

Generate Node Module

A command-line tool (CLI) for quickly scaffolding an Express.js module, generating basic TypeScript files with boilerplate code for controllers, services, routes, interfaces, constants, and validations.

Installation

To install the package globally, run the following command:

npm install -g generate-node-module

This allows you to run the generate-module command from anywhere on your system.

Usage

Once the package is installed, you can generate a new module by running the following command:

generate-module <module-name>

Example

To generate a module named admin, you would run:

generate-module admin

Output Structure

This command will create a folder named admin in the current working directory. Inside the admin folder, the following files will be generated:

admin/
│
├── admin.constant.ts      # Contains constants related to the module
├── admin.controller.ts    # Contains the controller logic for handling routes
├── admin.interface.ts     # Defines interfaces for the module
├── admin.model.ts         # Contains the Mongoose schema and model
├── admin.route.ts         # Sets up the routes for the module
├── admin.services.ts      # Service logic related to the module
├── admin.utils.ts         # Utility functions related to the module
└── admin.validation.ts    # Validation logic for request payloads

Each file comes with some basic template code to help you get started quickly.

Generated Code

Here is the template code that will be auto-generated inside each file.

1. admin.constant.ts

export const AdminConstants = { };

This file contains constants related to the module. You can add more constants specific to your admin module here.

2. admin.controller.ts

export const AdminController = { }

This file is the controller, where you will handle incoming HTTP requests for the admin routes.

3. admin.interface.ts

export interface AdminInterface {}

This file defines the interfaces for the module. You can extend or modify the interface to match the requirements of your application.

4. admin.model.ts

import mongoose from 'mongoose';

const AdminSchema = new mongoose.Schema({});
   
export const AdminModel = mongoose.model('admin', AdminSchema);

The model file contains the Mongoose schema and model for the admin module. You can define the schema fields and data types as needed.

5. admin.route.ts

import express from 'express'; 
    
const router = express.Router(); 
    
router.get('/'); 
    
export const AdminRoutes = router;

The route file defines how requests will be routed to the corresponding controller logic. You can add more routes as necessary.

6. admin.services.ts

export const AdminService = {};

This file contains business logic related to the admin module. It handles database calls and other services.

7. admin.utils.ts

export const AdminUtils = {};

This file contains utility functions related to the admin module.

8. admin.validation.ts

export const AdminValidation = {};

This file contains validation logic for the module.

How to Integrate the Generated Module

Once the module is generated, follow these steps to integrate it into your Express application:

  1. Import the Route in Your App:

    Open your main app.ts or server.ts file, where you configure your routes, and import the generated route file.

    import adminRoute from './admin/admin.route';
       
    app.use('/api', adminRoute);
  2. Use the Routes:

    Now, when your server is running, you can access the admin routes via http://localhost:<port>/api/admin.

Examples

Generating a User Module

To generate a user module, simply run:

generate-module user

This will create a user/ folder with the same structure and files as shown above, but specific to the user module.

Adding Routes to App

  1. Import the generated user.route.ts file in your app.ts:

    import userRoute from './user/user.route';
    app.use('/api', userRoute);
  2. Access the route at http://localhost:<port>/api/user.

Customizing the Boilerplate Code

Feel free to customize the generated files to fit your business logic. The tool provides a basic structure, but you can easily expand upon it.

Versioning

This project uses Semantic Versioning.

Authors

Md Hasibul Hasan

License

This project is licensed under the MIT License - see the LICENSE file for details.