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.
- 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.
Install the Nextpress library using npm (never yarn 😡):
npm install nextpress-router-
Create an
appfolder in your project's root directory. NextPress will scan this folder to find your route files. Inside theappfolder, 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");
});Create a basic route handler for a GET request at the root ("/"):
- In the
appfolder, 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
appfolder, create a new folder calledapiand 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".
NextPress makes it easy to apply middleware templates to your routes. To apply a global middleware to all routes:
- In the
appfolder, 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
appfolder, 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.tsand 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.
NextPress supports dynamic routes, similar to Next.js. To create a dynamic route:
- In the
appfolder, 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.jsand export a route handler function:
module.exports = [
(req, res) => {
res.send(`This is a dynamic route. The slug is: ${req.params.slug}`);
},
];NextPress supports route grouping, similar to Next.js. To create a route group:
- In the
appfolder, 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.jsand 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.jsfile is in the group subfolder, the group name is not included in the route path. The route path will be/(not/auth).
Contributions are always welcome! Please feel free to submit pull requests or open issues to help improve NextPress.