Comments (4)
Does this occur when you assign a route-specific middleware for a route with a "/" pattern?
Would you possibly be able to provide a code snippet to recreate the behavior?
This sounds like it can be a bug If it is being created without the use()
call as the only time a middleware should run on all routes is if it is assigned with a "/" pattern on a root instance of a Router
/Server
.
from hyper-express.
I fixed it in my repo(piliugin-anton/uQuik@c4d96c2#diff-3d79637b3dbe7416b63d43a0726d25d6cb614a2481a430c6cdff90dacea52a80)
Try this code for /test route:
const Server = require('./src/components/Server')
const server = new Server()
server.use((request, response, next) => {
console.log('global middleware')
next()
})
server.get('/', (request, response) => response.send('Hello /'))
server.use('/', (request, response, next) => {
console.log('/ middleware')
next()
})
server.get('/test', (request, response) => response.send('Hello /test'))
server.use('/test', (request, response, next) => {
console.log('test middleware')
next()
})
server
.listen(5000, '127.0.0.1')
.then((socket) => console.log('[server] Server started'))
.catch((error) => console.log('[server] Failed to start a server', error))
from hyper-express.
I reviewed your code and received the following output when performing a request to /
and then /test
This behavior is as expected because when you specify a pattern for a middleware, that pattern is treated as a wild card. So for example, a middleware that is used on a pattern /api
will be executed for all requests recieved over /api/*
hence when you bind a middleware on /
, you are effectively binding it for all requests on /*
aka. it acts as a global middleware in its modular scope. If you can find a different behavior for this use case in Express then feel free to make a PR and I'd be glad to include it but at the moment the documented behavior of Routers/Middlewares matches the behavior you encountered.
from hyper-express.
Thank you for explanation. I never used express before, this is why I was confused. I thought that idea of middlewares is to be executed before controller
from hyper-express.
Related Issues (20)
- HI kartik, I'm sorry for the stupid question, but I need to learn how to work with this by example, can you show me?) HOT 3
- Handling multiple multipart HOT 7
- Question: Is there support for HTTP3 / QUIC ? HOT 1
- Feature request: swagger HOT 1
- No error thrown if invalid json is sent as body HOT 4
- From version 6.0.0, the response component doesn´t call a close or abort event when the SSE client is closed HOT 1
- Nest-hyper-express-adapter middleware tests failing HOT 4
- Nest-hyper-express instance and local-pipes tests each fail one test HOT 3
- Outdated example of static serving / LiveDirectory HOT 1
- Error: uWS.HttpResponse must not be accessed after uWS.HttpResponse.onAborted callback HOT 1
- Graceful shutdown HOT 4
- Static serving HOT 1
- No error on binding to the same port HOT 1
- No Socket Received From uWebsockets.js likely due to an invalid host or busy port. HOT 2
- Optional Parameter Route HOT 2
- Read JSON as raw HOT 4
- Listen on IPv6 by default HOT 1
- Client certificate validation HOT 1
- v6.15.0 introduces breaking changes with the new server.listen unix socket method HOT 1
- Get Hyper-Express to feature parity with Express HOT 2
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 hyper-express.