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

rg-express

v0.9.90

Published

`rg-express` is a route generator library for Express inspired by Next.js and Express itself. It simplifies the process of setting up routes in an Express application, following a modular structure. This guide will walk you through the installation, setup

Downloads

49

Readme

rg-express User Guide

Introduction

rg-express is a route generator library for Express inspired by Next.js and Express itself. It simplifies the process of setting up routes in an Express application, following a modular structure. This guide will walk you through the installation, setup, and usage of rg-express in your project.

You can Download or Clone (Demo / Example project) with TypeScript

Installation

You can install rg-express using npm or yarn:

npm install rg-express
# or
yarn add rg-express

Project Structure

Basic Setup

Without src Directory

├── package.json
├── routes
├── app.ts or app.js
└── ...

or

├── package.json
├── src
│   ├── routes
│   ├── app.ts or app.js

in routes

 routes
   ├── product
   │      ├──route.ts ['/product']
   │      └──[slug]/route.ts ['/product/:slug']
   │                    └── (req.params.slug) // string
   │── hello
   │      └──[...slugs]
   │             └── route.ts ['/hello/:*']
   │                   └── (req.params.slugs) // string[]
   └── ...

Usage

Without src Directory

// app.js
const express = require('express');
const rg = require('rg-express');
const app = express();
app.use(rg.routes(__dirname));

app.listen(8001, () => {
  console.log('server is running at http://localhost:8001');
});

Route Configuration

// routes/hello/route.ts
/**
 * @api_endpoint : /hello/
 *
 */
export const GET = (req: Request, res: Response) => {
  res.send('hello rg-express ');
};

// --------------- Middlewares ----------------
const handlePost = (req: Request, res: Response) => {
  //  ...
};

// checkAuth is a normal expressjs Middleware function
export const POST = [checkAuth, handlePost];

or, with js

// routes/hello/route.js
/**
 * @api_endpoint : /hello/
 *
 */

module.exports.GET = (req, res) => {
  res.send('hello rg-express ');
};

// --------------- Middlewares ----------------
const handlePost = (req: Request, res: Response) => {
  //  ...
};

// checkAuth is a normal expressjs Middleware function
module.exports.POST = [checkAuth, handlePost];

routes/abc/route.ts

The routes in this project adhere to specific patterns to handle various scenarios:

This file handles the route for a specific scenario.

routes/abc/[slug]/route.ts --> [req.params.slug]

This route corresponds to paths such as /abc/something/. The [slug] notation denotes a dynamic parameter, representing a single value (e.g., /abc/example/). In your code, access this parameter using req.params.slug.

routes/abc/[...slugs]/route.ts --> [req.params.slugs]

This route is designed for paths with multiple dynamic parameters, where the [...slugs] notation signifies a variable number of values (e.g., /abc/first/second/third/). In your code, these values are accessible as an array: req.params.slugs.

Middlewares

You can directly assign functions for different HTTP methods with using any middlewares:

Middlewares

You can incorporate middlewares for different HTTP methods:

JavaScript

// route.js
module.exports.GET = [auth, getUser]; // Middleware for GET requests
module.exports.POST = [auth, authIsAdmin, newUser]; // Middleware for POST requests
module.exports.PUT = [auth, authIsAdmin, updateUser]; // Middleware for PUT requests
module.exports.DELETE = [auth, authIsAdmin, deleteUser]; // Middleware for DELETE requests

TypeScript

// route.ts
export const GET = [auth, getUser]; // Middleware for GET requests
export const POST = [auth, authIsAdmin, newUser]; // Middleware for POST requests
export const PUT = [auth, authIsAdmin, updateUser]; // Middleware for PUT requests
export const DELETE = [auth, authIsAdmin, deleteUser]; // Middleware for DELETE requests

Contributing

If you'd like to contribute to the project, please follow the guidelines outlined in the CONTRIBUTING.md file.

Conclusion

With rg-express, you can easily organize and set up routes in your Express application, making it more modular and scalable. Happy coding!

Happy coding!