Roufs (pronounced "roofs") is a straightforward and practical routing solution for Node.js API frameworks, inspired by the routing approach used in Next.js. This module is designed to work seamlessly with Express.js, but it can be easily adapted for use with other popular frameworks as well.
- Automatic route mapping
- Supports parameterized routes
- Minimal configuration required
To get started with Roufs in an express project, follow these steps:
-
Install Roufs:
npm install roufs
-
Create your route handler modules in the desired directory structure. For example:
- ./routes ├── index.js ├── authors │ ├── GET.js │ └── POST.js └── books ├── index.js └── [bookid].js
-
Integrate Roufs into your Express.js server in your
index.js
file:const express = require("express"); const roufs = require("roufs"); const app = express(); // Specify the path to your route handler modules app.use(roufs('./routes')); app.listen(5001, () => { console.log("Server is up and running on port 5001!"); });
Define your route handlers in separate modules within the ./routes
directory (relative to root of project). You can use simple functions or objects with HTTP method-specific functions to define your handlers. For example:
// Example 1: ./routes/authors/GET.js
module.exports = (req, res) => res.send("Response");
// Example 2: ./routes/books/index.js
module.exports = {
GET: (_, res) => res.send("GET:/books"),
POST: (_, res) => res.send("POST:/books")
}
// Example 3: ./routes/books/[bookid].js
module.exports = {
GET: (req, res) => res.send(req.params), // params = { bookid: "path-param" }
};
Roufs supports dynamic routes. You can create parameterized routes like /books/[bookid]
, and the module will handle them automatically.
Roufs uses a clever mechanism to traverse your route directory structure and dynamically match incoming requests to their corresponding handlers. It simplifies route management by abstracting away the complexities.
If you'd like to contribute to Roufs or have any questions, feel free to reach out. Your contributions and feedback are welcome.
This project is licensed under the MIT License - see the LICENSE file for details.
Please make sure to replace `"my-project"` with the actual name of your project and adjust the content as needed. Additionally, consider adding a license file (`LICENSE`) to your project directory and specifying the appropriate license for your module.