miniframe-router — a minimalist router for Express.JS applications, inspired by Ruby on Rails. Here’s how I made it.
Example of how routing might look in a simple application.
// Root routes root("index#home"); // Basic CRUD routes get("/users", "users#index"); get("/users/:id", "users#show"); post("/users", "users#create"); post("/users/:id", "users#update"); post("/users/:id/destroy", "users#destroy"); // Posts routes with scope scope("blog", () => { get("/posts", "posts#index"); get("/posts/:id", "posts#show"); post("/posts", "posts#create"); post("/posts/:id", "posts#update"); post("/posts/:id/destroy", "posts#destroy"); });
I hadn’t written NodeJS applications for about 10 years. Curious about Telegram Mini Apps, I decided to build a few simple projects to explore this new field.
For this, I needed routing for the backend of my applications. Existing solutions didn’t quite impress me, so I decided to create my own router — simple, effective, and inspired by Ruby on Rails. It was also a great opportunity to refresh my knowledge of creating NPM packages, something I hadn’t done in 9 years.
This simplifies the application structure and avoids cluttering the main application file.
import { root, get, post, routeScope as scope, getRouter, } from "miniframe-router"; // Root routes root("index#home"); // Basic CRUD routes get("/users", "users#index"); get("/users/:id", "users#show"); post("/users", "users#create"); post("/users/:id", "users#update"); post("/users/:id/destroy", "users#destroy"); // Posts routes with scope scope("blog", () => { get("/posts", "posts#index"); get("/posts/:id", "posts#show"); post("/posts", "posts#create"); post("/posts/:id", "posts#update"); post("/posts/:id/destroy", "posts#destroy"); }); export default getRouter;
A simple and effective entry point for the application: src/index.js or src/main.js.
import express from "express"; import getRouter from "./routes"; // <<< DEFINE ROUTES const app = express(); app.use(express.json()); app.use(getRouter()); // <<< USER ROUTES app.listen(3000, () => { console.log("Demo app is running on http://localhost:3000"); });
Controllers are located in the src/controllers directory.
MyApp └── src ├── controllers │ ├── blog │ │ └── postsController.ts │ ├── indexController.ts │ └── usersController.ts ├── index.ts └── routes └── index.ts
Example: src/controllers/usersController.ts (or .js).
// Root routes root("index#home"); // Basic CRUD routes get("/users", "users#index"); get("/users/:id", "users#show"); post("/users", "users#create"); post("/users/:id", "users#update"); post("/users/:id/destroy", "users#destroy"); // Posts routes with scope scope("blog", () => { get("/posts", "posts#index"); get("/posts/:id", "posts#show"); post("/posts", "posts#create"); post("/posts/:id", "posts#update"); post("/posts/:id/destroy", "posts#destroy"); });
Project code and documentation: GitHub - miniframe-router
NPM package: miniframe-router
Like, share, and subscribe! Constructive feedback is always welcome.
Author's page: GitHub - the-teacher
The above is the detailed content of miniframe-router: Router for Express.JS Applications. For more information, please follow other related articles on the PHP Chinese website!