问题
For my keystonejs project I have created two docker containers (one for mongo, one for the keystonejs app). All works well, with the exception that I am getting very often a "connection closed" error when browsing the site (usually when I haven't navigated for a couple of minutes). The full error stack is below:
Error thrown for request: /blog
Error: connection closed
at null.<anonymous> (/usr/src/app/node_modules/keystone/node_modules/mongoose/node_modules/mongodb/lib/mongodb/connection/server.js:618:45)
at emitNone (events.js:67:13)
at emit (events.js:166:7)
at null.<anonymous> (/usr/src/app/node_modules/keystone/node_modules/mongoose/node_modules/mongodb/lib/mongodb/connection/connection_pool.js:171:15)
at emitTwo (events.js:87:13)
at emit (events.js:172:7)
at Socket.<anonymous> (/usr/src/app/node_modules/keystone/node_modules/mongoose/node_modules/mongodb/lib/mongodb/connection/connection.js:550:12)
at emitOne (events.js:77:13)
at Socket.emit (events.js:169:7)
at TCP._onclose (net.js:477:12)
I have done some investigation, and another post on stackoverflow suggest to use the keepAlive option on mongoose (mongoose output the error "Error: connection closed"). However, I have no idea how I can use these options in keystonejs. Can anybody help?
Best regards, Tom
回答1:
In the keystonejs docs, there is mention of
mongoose Object | Instance of Mongoose to be used instead of the default instance.
So spawning your own may work. I just pulled the options mentioned at the answer you linked.
const mongoOptions =
{
db: {safe: true},
server: {
socketOptions: {
keepAlive: 1
}
},
replset: {
rs_name: 'myReplSet',
socketOptions: {
keepAlive: 1
}
}
};
mongoose.connect( YOUR_URI, mongoOptions );
keystone.set('mongoose', mongoose);
回答2:
I got it working like this:
var mongoose = require('mongoose');
mongoose.set('server', {
socketOptions: {
keepAlive: 1
}});
keystone.set('mongoose', mongoose);
Cheers, Tom
回答3:
If you just want to configure mongoose options for keystone to use, you can do so without starting your own mongoose instance.
You can add mongoose / mongo options using the undocumented 'mongo options' property of the keystone config, eg
keystone.init({
'mongo': 'yourMongoURI',
'mongo options': { server: { keepAlive: 1 }}
});
来源:https://stackoverflow.com/questions/37936021/mongo-connection-closed-in-keystonejs-app