我正在创建一个MEAN Stack应用程序,最近我包含了passportjs,起初我的Mongo数据库是1GB(它有300多万条目)一切都很好,但最近数据库增长到2GB,当护照是调用1/3次它会在控制台上抛出此错误:
(calling passport)
POST /login 200 3600.661 ms - 492 // successful login
GET /uploads/defaultm4.jpg 304 1.295 ms - -
(calling passport)
POST /login 200 3334.007 ms - 492 // successful login
(calling passport)
events.js:160
throw er; // Unhandled 'error' event
^
Error: connection timeout
at Db.<anonymous> (C:\Users\Andre\Desktop\bootcamp\voting-app\node_modules\m ongoose\lib\drivers\node-mongodb-native\connection.js:169:17)
at emitTwo (events.js:106:13)
at Db.emit (events.js:191:7)
at Server.listener (C:\Users\Andre\Desktop\bootcamp\voting-app\node_modules\ mongodb\lib\db.js:1786:14)
at emitOne (events.js:96:13)
at Server.emit (events.js:188:7)
at Server.<anonymous> (C:\Users\Andre\Desktop\bootcamp\voting-app\node_modul es\mongodb\lib\server.js:274:14)
at emitOne (events.js:96:13)
at Server.emit (events.js:188:7)
at Pool.<anonymous> (C:\Users\Andre\Desktop\bootcamp\voting-app\node_modules \mongodb-core\lib\topologies\server.js:334:12)
at emitOne (events.js:96:13)
at Pool.emit (events.js:188:7)
at Connection.<anonymous> (C:\Users\Andre\Desktop\bootcamp\voting-app\node_m odules\mongodb-core\lib\connection\pool.js:270:12)
at Connection.g (events.js:291:16)
at emitTwo (events.js:106:13)
at Connection.emit (events.js:191:7)
at Socket.<anonymous> (C:\Users\Andre\Desktop\bootcamp\voting-app\node_modul es\mongodb-core\lib\connection\connection.js:183:10)
at Socket.g (events.js:291:16)
at emitNone (events.js:86:13)
at Socket.emit (events.js:185:7)
at Socket._onTimeout (net.js:339:8)
at ontimeout (timers.js:365:14)
npm ERR! Windows_NT 10.0.14393
npm ERR! argv "C:\\Program Files\\nodejs\\node.exe" "C:\\Users\\Andre\\AppData\\ Roaming\\npm\\node_modules\\npm\\bin\\npm-cli.js" "start"
npm ERR! node v6.9.2
npm ERR! npm v3.9.0
npm ERR! code ELIFECYCLE
npm ERR! votingApp@0.0.0 start: `node ./bin/www`
npm ERR! Exit status 1
npm ERR!
npm ERR! Failed at the votingApp@0.0.0 start script 'node ./bin/www'.
npm ERR! Make sure you have the latest version of node.js and npm installed.
npm ERR! If you do, this is most likely a problem with the votingApp package,
npm ERR! not with npm itself.
npm ERR! Tell the author that this fails on your system:
npm ERR! node ./bin/www
npm ERR! You can get information on how to open an issue for this project with:
npm ERR! npm bugs votingApp
npm ERR! Or if that isn't available, you can get their info via:
npm ERR! npm owner ls votingApp
npm ERR! There is likely additional logging output above.
npm ERR! Please include the following file with any support request:
npm ERR! C:\Users\Andre\Desktop\bootcamp\voting-app\npm-debug.log
这是我的App.js:
var express = require('express');
var session = require('express-session')
var http = require('http');
var path = require('path');
var favicon = require('serve-favicon');
var logger = require('morgan');
var cookieParser = require('cookie-parser');
var bodyParser = require('body-parser');
var app = express();
var mongoose = require('mongoose');
var passport = require('passport');
var options = {server: {socketOptions: {connectionTimeoutMS: 120000,socketTimeoutMS: 120000}}};
mongoose.connect('mongodb://localhost:27017/mean-database',options, function(err,db){
if (!err){
console.log('Connected to /mean-database!');
} else{
console.dir(err); //failed to connect
}
});
require('./models/citizens');
require('./config/passport');
require('./models/Candidate');
require('./models/Province');
require('./models/Parties');
var routes = require('./routes/index');
var citizens = require('./routes/citizens');
var candidates = require('./routes/candidates');
var provinces = require('./routes/provinces');
var graphics = require('./routes/graphics');
var parties = require('./routes/parties');
// view engine setup
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'ejs');
app.use(favicon(__dirname + '/public/favicon.ico'));
app.use(logger('dev'));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));
app.use(cookieParser());
app.use(express.static(path.join(__dirname, 'public')));
app.use(passport.initialize());
app.use('/', routes);
app.use('/users', citizens);
app.use('/api', candidates);
app.use('/api', provinces);
app.use('/api', graphics);
app.use('/api', citizens);
app.use('/api', parties);
// catch 404 and forward to error handler
app.use(function(req, res, next) {
var err = new Error('Not Found');
err.status = 404;
next(err);
});
// error handlers
// development error handler
// will print stacktrace
if (app.get('env') === 'development') {
app.use(function(err, req, res, next) {
res.status(err.status || 500);
res.render('error', {
message: err.message,
error: err
});
});
}
// production error handler
// no stacktraces leaked to user
app.use(function(err, req, res, next) {
res.status(err.status || 500);
res.render('error', {
message: err.message,
error: {}
});
});
module.exports = app;
这是我的“开始”脚本:
var debug = require('debug')('votingApp');
var app = require('../app');
app.set('port', process.env.PORT || 3000);
var server = app.listen(app.get('port'), function() {
debug('Express server listening on port ' + server.address().port);
});
这是我的passport.js文件:
var express = require('express');
var passport = require('passport');
var LocalStrategy = require('passport-local').Strategy;
var mongoose = require('mongoose');
var User = mongoose.model('Citizen');
passport.use(new LocalStrategy(
function(electoral_code, password, done) {
User.findOne({ electoral_code: electoral_code }, function (err, user) {
if (err) { return done(err); }
if (!user) {
return done(null, false, { message: 'Incorrect username.' });
}
if (!user.validPassword(password)) {
return done(null, false, { message: 'Incorrect password.' });
}
return done(null, user);
}).maxTime(20000);
}
));
我已经尝试过:重新安装节点,更新所有节点模块,清理缓存,重新安装所有模块
编辑:还添加了npm-debug.log:
0 info it worked if it ends with ok
1 verbose cli [ 'C:\\Program Files\\nodejs\\node.exe',
1 verbose cli 'C:\\Users\\Andre\\AppData\\Roaming\\npm\\node_modules\\npm\\bin\\npm-cli.js',
1 verbose cli 'start' ]
2 info using npm@3.9.0
3 info using node@v6.9.2
4 verbose run-script [ 'prestart', 'start', 'poststart' ]
5 info lifecycle votingApp@0.0.0~prestart: votingApp@0.0.0
6 silly lifecycle votingApp@0.0.0~prestart: no script for prestart, continuing
7 info lifecycle votingApp@0.0.0~start: votingApp@0.0.0
8 verbose lifecycle votingApp@0.0.0~start: unsafe-perm in lifecycle true
9 verbose lifecycle votingApp@0.0.0~start: PATH: C:\Users\Andre\AppData\Roaming\npm\node_modules\npm\bin\node-gyp-bin;C:\Users\Andre\Desktop\bootcamp\voting-app\node_modules\.bin;C:\Program Files\nodejs;C:\Users\Andre\bin;C:\Program Files\Git\mingw64\bin;C:\Program Files\Git\usr\local\bin;C:\Program Files\Git\usr\bin;C:\Program Files\Git\usr\bin;C:\Program Files\Git\mingw64\bin;C:\Program Files\Git\usr\bin;C:\Users\Andre\bin;C:\ProgramData\Oracle\Java\javapath;C:\Program Files (x86)\Intel\iCLS Client;C:\Program Files\Intel\iCLS Client;C:\WINDOWS\system32;C:\WINDOWS;C:\WINDOWS\System32\Wbem;C:\WINDOWS\System32\WindowsPowerShell\v1.0;C:\Program Files\Intel\Intel(R) Management Engine Components\DAL;C:\Program Files (x86)\Intel\Intel(R) Management Engine Components\DAL;C:\Program Files\Intel\Intel(R) Management Engine Components\IPT;C:\Program Files (x86)\Intel\Intel(R) Management Engine Components\IPT;C:\Program Files (x86)\Windows Kits\8.1\Windows Performance Toolkit;C:\Program Files\Microsoft SQL Server\110\Tools\Binn;C:\Program Files (x86)\Microsoft SDKs\TypeScript\1.0;C:\Program Files\Microsoft SQL Server\120\Tools\Binn;C:\Program Files (x86)\Skype\Phone;C:\Program Files (x86)\OpenSSH\bin;C:\Program Files (x86)\Heroku\bin;C:\Program Files (x86)\git\cmd;C:\Program Files\Git\cmd;C:\Program Files\nodejs;C:\Program Files (x86)\Microsoft VS Code\bin;C:\Users\Andre\AppData\Local\Microsoft\WindowsApps;C:\Users\Andre\AppData\Local\atom\bin;C:\Users\Andre\AppData\Roaming\npm;C:\Program Files\Git\usr\bin\vendor_perl;C:\Program Files\Git\usr\bin\core_perl
10 verbose lifecycle votingApp@0.0.0~start: CWD: C:\Users\Andre\Desktop\bootcamp\voting-app
11 silly lifecycle votingApp@0.0.0~start: Args: [ '/d /s /c', 'node ./bin/www' ]
12 silly lifecycle votingApp@0.0.0~start: Returned: code: 1 signal: null
13 info lifecycle votingApp@0.0.0~start: Failed to exec start script
14 verbose stack Error: votingApp@0.0.0 start: `node ./bin/www`
14 verbose stack Exit status 1
14 verbose stack at EventEmitter.<anonymous> (C:\Users\Andre\AppData\Roaming\npm\node_modules\npm\lib\utils\lifecycle.js:245:16)
14 verbose stack at emitTwo (events.js:106:13)
14 verbose stack at EventEmitter.emit (events.js:191:7)
14 verbose stack at ChildProcess.<anonymous> (C:\Users\Andre\AppData\Roaming\npm\node_modules\npm\lib\utils\spawn.js:24:14)
14 verbose stack at emitTwo (events.js:106:13)
14 verbose stack at ChildProcess.emit (events.js:191:7)
14 verbose stack at maybeClose (internal/child_process.js:877:16)
14 verbose stack at Process.ChildProcess._handle.onexit (internal/child_process.js:226:5)
15 verbose pkgid votingApp@0.0.0
16 verbose cwd C:\Users\Andre\Desktop\bootcamp\voting-app
17 error Windows_NT 10.0.14393
18 error argv "C:\\Program Files\\nodejs\\node.exe" "C:\\Users\\Andre\\AppData\\Roaming\\npm\\node_modules\\npm\\bin\\npm-cli.js" "start"
19 error node v6.9.2
20 error npm v3.9.0
21 error code ELIFECYCLE
22 error votingApp@0.0.0 start: `node ./bin/www`
22 error Exit status 1
23 error Failed at the votingApp@0.0.0 start script 'node ./bin/www'.
23 error Make sure you have the latest version of node.js and npm installed.
23 error If you do, this is most likely a problem with the votingApp package,
23 error not with npm itself.
23 error Tell the author that this fails on your system:
23 error node ./bin/www
23 error You can get information on how to open an issue for this project with:
23 error npm bugs votingApp
23 error Or if that isn't available, you can get their info via:
23 error npm owner ls votingApp
23 error There is likely additional logging output above.
24 verbose exit [ 1, true ]
答案 0 :(得分:1)
(代表OP发布)。
我通过索引Mongo数据库解决了这个问题:
db.citizens.ensureIndex({ username: 1, password: 1 })