# nextpress-router

> A Next.JS app router like ExpressJS controller architecture.

Latest version **1.1.0** (published 2023-10-11) · MIT license · 0 weekly downloads

## Install

```sh
npm install nextpress-router
pnpm add nextpress-router
yarn add nextpress-router
bun add nextpress-router
```

## Health

**Score 20/100 (F)** — status: abandoned.

Positive: has types; no vulnerabilities.

Warnings: low downloads; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.1.0 |
| Published | 2023-10-11 |
| First published | 2023-06-03 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | CommonJS |
| Dependencies | 1 |
| Unpacked size | 29.1 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 5 |
| Author | Archer Hume |
| Maintainers | archerhume |
| Keywords | nextjs, expressjs, routing, file-based routing |

## Links

- npm: https://www.npmjs.com/package/nextpress-router
- Repository: https://github.com/ArcherHume/Nextpress
- Homepage: https://github.com/ArcherHume/Nextpress#readme
- Issues: https://github.com/ArcherHume/Nextpress/issues
- npm.io page: https://npm.io/package/nextpress-router

## Dependencies (1)

- [express](https://npm.io/package/express.md) ^4.18.2

## Alternatives

- [express-promise-router](https://npm.io/package/express-promise-router.md) — 736.1K weekly downloads
- [next-usequerystate](https://npm.io/package/next-usequerystate.md) — 29.8K weekly downloads
- [@bitkyc08/opencodex](https://npm.io/package/@bitkyc08/opencodex.md) — 4.6K weekly downloads
- [lynkr](https://npm.io/package/lynkr.md) — 575 weekly downloads
- [baremetal.js](https://npm.io/package/baremetal.js.md) — 42 weekly downloads

## Recent versions

- 1.1.0 (latest) — 2023-10-11
- 1.0.9 — 2023-08-27
- 1.0.8 — 2023-08-27
- 1.0.7 — 2023-08-27
- 1.0.6 — 2023-08-27
- 1.0.5 — 2023-06-15
- 1.0.4 — 2023-06-03
- 1.0.3 — 2023-06-03
- 1.0.2 — 2023-06-03
- 1.0.1 — 2023-06-03
- 1.0.0 — 2023-06-03

## README

# Nextpress

Nextpress is a lightweight Node.js library that allows developers to handle [Express.js](https://expressjs.com/) routing like a [Next.js](https://nextjs.org/) 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 😡):

```bash
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`):

```javascript
const express = require("express");
const nextpress = require("nextpress-router");
```

3. Create an Express app:

```javascript
const app = express();
```

4. Initialize NextPress:

```javascript
// 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 });
```

5. Start the server:

```javascript
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:

```javascript
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:

```javascript
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:
```javascript
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:

```javascript
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:

```javascript
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:

```javascript
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](LICENSE)

---
_Source: https://npm.io/package/nextpress-router · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
