Reputation: 45
Is it correct to end the request-response cycle within a middleware? In the sense if is it a good practice? Or which should be the optimum solution, for example in my next code, I end the req-res cycle withing the middleware
app.get('/api/shorturl/:short_url',(req,res,next)=>{
url_model.find({short_url: req.params.short_url}).then(doc =>{
res.json(doc); //here I'm closing the req-res within the middleware
}).catch(err =>next(err));
})
But I was taught, that a middleware was supposed to handle the req-res and prepare it for future middlewares or the route handler itself.
Upvotes: 0
Views: 129
Reputation: 708096
In Express, there's not really a hard demarcation between what is middleware and what is a request handler. You can use app.use()
or app.get()
or any other app.xxx()
for either middleware-type behavior or request handler-type behavior. It just depends entirely upon how you write your handler function and what you intend for the behavior to be.
Your particular app.get()
code in your question is behaving more as a request handler. It processes the incoming request and either sends a response (thus "handling" the request) or forwards an error to the centralized error handler which will then "handle" the request. This is 100% normal design in Express.
Is it correct to end the request-response cycle within a middleware?
Yes, you can end the request-response cycle within middleware if that's the appropriate logic for your server.
Classic middleware would be something like:
app.use(express.json());
That is just examining the request to see if it's a request it wants to participate in, preprocessing the request and setting some state on the req
object so that some code later in the chain can finish the response or possibly encountering an error (such as invalid JSON) and then forwarding that error to the centralized error handler.
But, there can also be hybrid middleware designs that sometimes send an immediate response and finish the request and sometimes set some state on the req
object and then continue routing by calling next()
so that other handlers later in the chain can finish the response.
Within Express, there is no hard and fast line between the two. You can do either a middleware-type behavior or a request handler-type behavior (that finishes the response) from any Express handler. So, calling something middleware vs. a request handler is really just semantics and perhaps a label that attempts to describe what it "mostly" does.
But I was taught, that a middleware was supposed to handle the req-res and prepare it for future middlewares or the route handler itself.
Classic middleware does what you say. But, the app.get()
code you show is more like a classic request handler (not like middleware). It has a specific endpoint (with a wildcard last path segment) it's looking for and it "handles" the response for that endpoint. But, as I said above with Express, there is no hard and fast differentiation between the two so don't get caught up in semantics.
FYI, this:
.catch(err =>next(err));
can be shortened to this:
.catch(next);
Upvotes: 1