nextpress-router
v1.1.0
Published
A Next.JS app router like ExpressJS controller architecture.
Downloads
14
Maintainers
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
Create an
app
folder in your project's root directory. NextPress will scan this folder to find your route files. Inside theapp
folder, create files named with the desired HTTP method (e.g.,get.js
,post.js
, etc.).Import Express and Nextpress in your server file (e.g.,
index.ts
):
const express = require("express");
const nextpress = require("nextpress-router");
- Create an Express app:
const app = express();
- 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 });
- 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 ("/"):
- In the
app
folder, create a file namedget.js
. - 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"):
- In the
app
folder, create a new folder calledapi
and inside it, create a file namedpost.js
. - 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:
- In the
app
folder, create a file namedmiddlewares.ts
. - 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:
- 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. - 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:
- In the
app
folder, create a folder such as [slug] (the name of the folder should be surrounded by brackets). - 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:
- 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. - 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.