Say I have app.js as the main app on the server, and it imports(requires) 4 other scripts that route login, join, write article, delete article requests. I currently have connection instances through
var connection = mysql.createConnection(mysqlConfig)
in EACH of the login.js, join.js, write.js, delete.js.
I have a feeling that this is bad practice. What should I do? Should I create a mysqlconnection.js that exports a connection instance, and require that in each of the scripts that require a connection?
Asked
Active
Viewed 139 times
0

Gyuhyeon Lee
- 878
- 1
- 9
- 20
-
3Please try connection pooling. This will help you http://stackoverflow.com/questions/18496540/node-js-mysql-connection-pooling – kinkajou Jul 16 '16 at 01:00
1 Answers
1
Deciding the number of connections depends on your application. If you have a sporadically used application with very few calls to mysql, then a single connection might work. However, looking at your use case, it seems that multiple users will be trying to login, write, etc. In such a case I recommend using Connection Pooling. https://github.com/coopernurse/node-pool

Naved Alam
- 11
- 2
-
`mysql` has [built-in connection pooling](https://www.npmjs.com/package/mysql#pooling-connections). – robertklep Jul 16 '16 at 06:58