Giter Club home page Giter Club logo

Comments (4)

kartikk221 avatar kartikk221 commented on May 23, 2024

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.

piliugin-anton avatar piliugin-anton commented on May 23, 2024

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.

kartikk221 avatar kartikk221 commented on May 23, 2024

I reviewed your code and received the following output when performing a request to / and then /test
1b277d1898373bf8df02feda3211fa97

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.

piliugin-anton avatar piliugin-anton commented on May 23, 2024

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)

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.