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

nextpress-router

v1.1.0

Published

A Next.JS app router like ExpressJS controller architecture.

Downloads

35

Readme

Nextpress

Nextpress is a lightweight Node.js library that allows developers to handle Express.js routing like a Next.js app router.

It simplifies the process of setting up and managing routes and middlewares in an Express.js application, by providing a structure similar to Next.js, keeping your code organized and easy to maintain.

Features

  • Automatically loads routes based on file structure.
  • Supports dynamic routes.
  • Supports route grouping.
  • Supports middleware templating, similar to Next.js' layout functionality.
  • Supports hot reloading of routes and middlewares.

Getting Started

Installation

Install the Nextpress library using npm (never yarn 😡):

npm install nextpress-router

Basic Usage

  1. Create an app folder in your project's root directory. NextPress will scan this folder to find your route files. Inside the app folder, create files named with the desired HTTP method (e.g., get.js, post.js, etc.).

  2. Import Express and Nextpress in your server file (e.g., index.ts):

const express = require("express");
const nextpress = require("nextpress-router");
  1. Create an Express app:
const app = express();
  1. Initialize NextPress:
// The verbose option is optional and will display loading and route information in the console if set to true
// The hotReload option is optional and will reload the routes when a file is changed if set to true
nextpress.init(app, { verbose: true, hotReload: true });
  1. Start the server:
app.listen(3000, () => {
  console.log("Server listening on port 3000");
});

Examples

Create a basic route handler for a GET request at the root ("/"):

  1. In the app folder, create a file named get.js.
  2. In get.js, export a route handler function:
module.exports = [
    (req, res) => {
        res.send("Hello, Nextpress!");
    }
];

Now, when you start the server and navigate to http://localhost:3000, you should see the message "Hello, Nextpress!".

To create a route handler for a POST request at a different path (e.g., "/api/data"):

  1. In the app folder, create a new folder called api and inside it, create a file named post.js.
  2. In post.js, export a route handler function:
module.exports = [(req, res) => {
  res.send("Handling a POST request at /api/data");
}];

Now when you send a POST request to http://localhost:3000/api/data, you should receive the message "Handling a POST request at /api/data".

Middlewares

NextPress makes it easy to apply middleware templates to your routes. To apply a global middleware to all routes:

  1. In the app folder, create a file named middlewares.ts.
  2. In middlewares.ts, export a middleware function:
module.exports = [
  (req, res, next) => {
    console.log("This is a global middleware");
    next();
  },
];

To apply middleware(s) to a specific group of routes:

  1. In the app folder, create a new folder with the desired group name surrounded by parentheses (e.g., (auth)). This tells NextPress that this folder represents a route group.
  2. Inside the new folder, create a file called middlewares.ts and export a middleware function:
module.exports = [
  (req, res, next) => {
    console.log("This middleware only applies to the auth group");
    next();
  },
];

Any route files within this group folder (e.g., get.js, post.js, etc.) will have this middleware applied.

🚨 Note: When a route has multiple applicable middleware files, the middleware file will be chosen based on it's proximity to the route file. For example, a middleware file located in the same folder as the route file will take precedence over a middleware file located in the parent folder.

Dynamic Routes

NextPress supports dynamic routes, similar to Next.js. To create a dynamic route:

  1. In the app folder, create a folder such as [slug] (the name of the folder should be surrounded by brackets).
  2. Inside the new folder, create a file called get.js and export a route handler function:
module.exports = [
    (req, res) => {
        res.send(`This is a dynamic route. The slug is: ${req.params.slug}`);
    },
];

Route Groups

NextPress supports route grouping, similar to Next.js. To create a route group:

  1. In the app folder, create a folder with the desired group name surrounded by parentheses (e.g., (auth)). This tells NextPress that this folder represents a route group.
  2. Inside the new folder, create a file called get.js and export a route handler function:
module.exports = [
    (req, res) => {
        res.send("This route is part of the auth group");
    },
];

💡 NOTE: Even though the route get.js file is in the group subfolder, the group name is not included in the route path. The route path will be / (not /auth).

Contributing

Contributions are always welcome! Please feel free to submit pull requests or open issues to help improve NextPress.

License

MIT