I'm working with node.js and socket.io using mysql module in it. Is it possible to declare
var connection = mysql.createConnection(database);
as global? rather using it in all functions, so that i can use it in all functions even if each function has
connection.end();
in it??
node.js has GLOBAL
as the global to app. So you can do like this.
if (!GLOBAL.connection) {
GLOBAL.connection = mysql.createConnection(database);
}
and when you like to end connection, use
if (GLOBAL.connection)
GLOBAL.connection.end();
您可以避免使用全局变量并使用此初始化选项,以保持全局空间的清洁: https : //stackoverflow.com/a/18008471/491662
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.