1: C:\Node_app\microblog\node_modules\connect-mongo\lib\connect-mongo.js:126
2: throw new Error('Error connecting to database');
3: ^
4: Error: Error connecting to database
5: at module.exports.MongoStore.__proto__ (C:\Node_app\microblog\node_module
6: at Db.open (C:\Node_app\microblog\node_modules\connect-mongo\node_modules
7: at Server.connect.connectionPool.on.server._serverState (C:\Node_app\micr
8: ion\server.js:408:7)
9: at EventEmitter.emit (events.js:115:20)
10: at connection.on.connectionStatus (C:\Node_app\microblog\node_modules\con
11: :93:15)
12: at EventEmitter.emit (events.js:91:17)
13: at Socket.errorHandler (C:\Node_app\microblog\node_modules\connect-mongo\
14: at Socket.EventEmitter.emit (events.js:88:17)
15: at Socket._destroy.self.errorEmitted (net.js:329:14)
16: at process.startup.processNextTick.process._tickCallback (node.js:244:9)
It keeps reporting errors in Db.open
, check the source code, and find that there should be err
parameters passed in, because I have never contacted mongodb before - I have seen a little bit of understanding in some articles, and I feel that mongodb is not installed The problem? Tried it, and it really turned out to be the case.
Download MongoDB, download page >>
Unzip and install, because the C drive of my computer is not the system disk, and the default is my working directory, so I first unzip it to this disk. c:\mongodb
(decompress it, there is a bin/
directory with many .exe
files), and then create a directory and file c: \mongodb\logs\mongodb.log
Create a MongoDB data storage directory, assuming it is c:\mongodb_data\db
Start the Mongo Db service. If you see similar information on the console, the startup is successful
The default MongoDB listening port is 27017
, and mysql's port is 3306
Cmd to open a new window into the mongodb bin /
directory, input mongo.exe
, the test described by the following information
C:\mongodb\bin>mongo
MongoDB shell version:2.0.6
connection to: test
continue test:
1: >use test
2: sitched to db test
3: >db.foo.save({hello:1, word:2})
4: >db.foo.find()
5: "_id" : ObjectId("500a5fde4ad5d4c1884a5c3f"), "hello" : 1, "word" : 2 }
Then enter exit
to exit the window, and then enter exit
to close the dos.
1: C:\mongodb\bin>mongod.exe --dbpath=c:\mongodb_data\db --logpath=c:\mongodb\logs\mongodb.log --install
If you need to uninstall the service, use sc delete MongoDB
net start MongoDB
Open the task manager, you can see that the process has started
If you need to stop the service: net stop MongoDb
Start the app again and find that it can be started normally.
At this point, using mongodb in express
to save session
can work normally, the code of app.js
is as follows:
1:
2: /**
3: * Module dependencies.
4: */
5:
6: var express = require('express');
7: var http = require('http');
8: var routes = require('./routes');
9:
10: var settings = require('./settings');
11:
12: var MongoStore = require('connect-mongo')(express);
13: //var connect = require('connect');
14: //var MongoStore = require('connect-mongo')(connect);
15:
16:
17: var sessionStore = new MongoStore({
18: db : settings.db
19: }, function() {
20: console.log('connect mongodb success...');
21: });
22:
23:
24:
25: var app = express();
26:
27: app.configure(function(){
28: app.set('port', process.env.PORT || 3000);
29: app.set('views', __dirname + '/views');
30: app.set('view engine', 'jade');
31: app.use(express.favicon());
32: app.use(express.logger('dev'));
33: app.use(express.bodyParser());
34: app.use(express.methodOverride());
35:
36: app.use(express.cookieParser());
37:
38: app.use(express.session({
39: secret : settings.cookie_secret,
40: store : sessionStore,
41: cookie : {
42: maxAge : new Date(Date.now() + 1000 * 60 * 60)
43: }
44: }));
45:
46: app.use(app.router);
47: app.use(express.static(__dirname + '/public'));
48: });
49:
50: app.configure('development', function(){
51: app.use(express.errorHandler());
52: });
53:
54: app.get('/', routes.index);
55:
56:
57: http.createServer(app).listen(app.get('port'), function(){
58: console.log("Express server listening on port " + app.get('port'));
59: });