Comments (8)
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.
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.
tinyhttp/packages/router/src/index.ts
Lines 212 to 218 in 54d95f0
from tinyhttp.
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.
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.
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.
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.
Middlewares and settings wont work
Yes, that is due to the fact that route
returns a new instance.
tinyhttp/packages/app/src/app.ts
Lines 264 to 270 in 54d95f0
from tinyhttp.
tinyhttp author here
a few things:
-
@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 bothapp
androuter
. Just use theapp
package. -
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)
- 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)
- Add declarationMap:true to tsconfig for correct ctrl-click go-to-source HOT 5
- App is seen with type "any" when imported HOT 5
- res.sendFile should respect content type when set HOT 3
- Add .nvmrc to project root HOT 1
- Response object interface doesn't match @types/express behavior, it's not really generic HOT 6
- Simple Pull Request - License-none
- Failed to start https server HOT 1
- Multiple path arguments (as array) to route HOT 4
- Link CONTRIBUTING.md in README.md and mention forking as the first step instead of clone HOT 1
- Incompatible with Express middleware: `express.Handler` HOT 26
- Mounting a subapp on array of paths
- Add missing `accepts` TypeScript signatures HOT 4
- Mallformed URI's leads to crash with `/thing/:id` routes HOT 1
- Exporting a router file is not working HOT 7
- Create a community server HOT 8
- The website is down HOT 7
- Request `.xhr` misses fetch requests (headers are lowercase) HOT 3
- req.route always returns undefined HOT 19
- Server instance (of `@tinyhttp/app`) hangs on test since 2.0.33 HOT 4
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from tinyhttp.