var http = require(‘http’); var mongodb = require(‘mongodb’); var poolModule = require(‘generic-pool’);
var pool = poolModule.Pool({
name: 'mongodb',
create: function(callback) {
mongodb.MongoClient.connect('mongodb://localhost/test', {
server: {poolSize: 10}
}, function(err, db) {
callback(err, db);
});
},
destroy: function(db) {
db.close();
},
max: 100,
idleTimeoutMillis: 30000,
log: false
});
http.createServer(function(req, res) {
pool.acquire(function(err, db) {
if (err) {
res.statusCode = 500;
res.end(JSON.stringify(err, null, 2));
} else {
db.collection('userinfo').save({name: 'jack'}, function(err, result) {
res.end(JSON.stringify(result, null , 2));
pool.release(db);
});
}
});
}).listen('8888');
console.log('this server is listening on port 8888...');
setTimeout(function() {
for (var i = 0; i < 1000; i++) {
http.get('http://localhost:8888', function(res) {
console.log('request ok...');
});
}
}, 2000);
错误信息:
events.js:85
throw er; // Unhandled 'error' event
^
Error: connect ECONNREFUSED
at exports._errnoException (util.js:746:11)
at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1000:19)