GithubHelp home page GithubHelp logo

Comments (8)

hpandelo avatar hpandelo commented on May 24, 2024

Also good to mention:

import { App, Request, Response } from '@tinyhttp/app'

const router = new App().route('/home') /* Won't register the /home */

router.get('/', (_req: Request, res: Response) => {
  res.status(200).json({ message: 'Welcome' })
})

export const MainRouter = router

To have a way to set the base/root path together into the Router file will be awesome

from tinyhttp.

aarontravass avatar aarontravass commented on May 24, 2024

Hi @hpandelo thanks for reporting these errors. I do have 1 thing to point out

// pushMiddleware(MainRouter.middleware) /* Not Working */

pushMiddleware is an internal method and is generally not supposed to be used by the user. Moreover, the syntax you have used it incorrect as well. See below for it's usage internally.

pushMiddleware<Req, Res>(this.middleware)({
path: arg,
handler: handlers[0],
handlers: handlers.slice(1),
method,
type: 'route'
})

from tinyhttp.

aarontravass avatar aarontravass commented on May 24, 2024

One way to use app.use(MainRouter.middleware) would be to extract the handler and then pass. Like this
app.use(MainRouter.middleware.map(m => m.handler))

from tinyhttp.

hpandelo avatar hpandelo commented on May 24, 2024

There are some ways to make it work, but won't be compatible with the usage in Express

It should be like:
app.use('/home', MainRouter) & app.use(MainRouter)

from tinyhttp.

aarontravass avatar aarontravass commented on May 24, 2024

but won't be compatible with the usage in Express

Yes. There's also an existing issues but that is related to types #387

from tinyhttp.

hpandelo avatar hpandelo commented on May 24, 2024

Another point, by using this way:
app.route('/home').middleware.push(...MainRouter.middleware)

Middlewares and settings wont work

export const app = new App({
  noMatchHandler: ...
  onError: ...
  settings: {
    networkExtensions: true,
    xPoweredBy: 'NeoX',
  },
})

xPoweredBy for example with a custom string or with false (also with app.use(helmet()) only will be affecting the routes from app.ts file, all the others "pushed" from Router() won't work (still returning the x-powered-by: tinyhttp)

from tinyhttp.

aarontravass avatar aarontravass commented on May 24, 2024

Middlewares and settings wont work

Yes, that is due to the fact that route returns a new instance.

route(path: string): App {
const app = new App()
this.use(path, app)
return app
}

from tinyhttp.

talentlessguy avatar talentlessguy commented on May 24, 2024

tinyhttp author here

a few things:

  1. @tinyhttp/router is an internal package and is a barebones router without most of the tinyhttp's logic. basically it's just a fancy thing to manage middleware arrays. you can't use both app and router. Just use the app package.

  2. app.use(<path>, <app>) is working as expected and passes in tests, see example https://github.com/tinyhttp/tinyhttp/blob/master/examples/subapps

basically the correct way is this:

const app = new App()
const subapp = new App()
subapp.get('/', (req, res) => res.json({ hello: 'world' }))
app.use(subapp)
  1. subapps created via app.route not respecting parent app's settings is a bug and should be filed as a separate issue (PR welcome)

if there's anything left unclear, pls lmk

from tinyhttp.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.