Answer the question
In order to leave comments, you need to log in
Node.js + mongoose. Whether to keep 1 connection or open / close for each request?
Good afternoon.
I recently started learning node.js.
As a database - mongodb. I use mongoose to work with it.
Initially created a new connection for each request and closed at the end. Sometimes there were errors like "connection closed" when a request was sent using an already closed connection. It is clear that this is due to my curvature in matters of asynchronous code. But I thought about this question. Is it worth opening a new connection for each request, or is it better to create one for all?
I understand correctly that when using only 1 connection, it will be used for all users. And if you create a new one every time, then these connections will work in parallel?
Do not hit if you asked stupidity))
Answer the question
In order to leave comments, you need to log in
Mongoose itself is smart enough to manage connections. See the documentation (section Connection pools)
Why do extra work for every request? Why the extra level of callbacks?
It must be borne in mind that node.js applications live for a long time, and are not launched on every request.
You will not manually add gasoline to the tank after each stroke of the internal combustion engine?
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question