hviana / faster

A fast and optimized middleware server with an absurdly small amount of code (300 lines) built on top of Deno's native HTTP APIs with no dependencies. It also has a collection of useful middlewares: log file, serve static, CORS, session, rate limit, token, body parsers, redirect, proxy and handle upload. In "README" there are examples of all the resources. Faster's ideology is: all you need is an optimized middleware manager, all other functionality is middleware.
MIT License
34 stars 8 forks source link

FR: add prefix option for .use() #6

Open Tzahile opened 9 months ago

Tzahile commented 9 months ago

Hi, Thanks for giving from your time and creating a small, super-fast server library for Deno!

I'd like to ask for a feature;

In express.js (and many others) you can set a prefix for the routes you define, in the following way:

import { usersRoute } from "./users.ts"
import { teamsRoute } from "./teams.ts"
import { v2Route } from "./v2.ts"

const route = new Route()
route.use('/users', usersRoute)
route.use('/teams', teamsRoute)
route.use('/api/v2', v2Route)

route.get(
  '/',
  res('json'),
  req('json'),
  async (ctx: any, next: any) => { ... } // etc
)

The use-case here is to give a prefix to all the routes in usersRoute, teamsRoute.. Then, if in usersRoute I defined the following route:

route.get(
  '/payments',
  res('json'),
  req('json'),
  async (ctx: any, next: any) => { ... } // etc
)

then it will be served in localhost:80/users/payments.

If you prefer me to work on it and create a PR, I'll be happy to. Thanks in advance

hviana commented 9 months ago

Hello, I will work on this feature. It is simple to implement. Will be available soon.

hviana commented 7 months ago

I added your feature with native JavaScript resources, see: https://github.com/hviana/faster#organizing-routes-in-files