2016-11-20 80 views
-1

我「當我運行的mongod m如果此消息。「等待在端口27017連接」連接到貓鼬

在我做的教程中,我想我應該得到的東西,如」連接從127.0接受.0.16:6。」

我檢查了這個職位已經 - mongod HostnameCanonicalizationWorker error on OS X

,我有我的主機文件設置爲這樣:

127.0.0.1  localhost 
127.0.0.1 Bens-MacBook-Pro.local 
255.255.255.255 broadcasthost 
::1    localhost 

這些都是我的爲節點腳本:

// Main starting point of the application 
const express = require('express'); 
const http = require('http'); 
const bodyParser = require('body-parser'); 
const app = express(); 
const morgan = require('morgan'); 
const router = require('./router'); 
const mongoose = require('mongoose'); 

// DB Setup 

mongoose.connect('mongodb://localhost:auth/auth'); 


// App Setup 
app.use(morgan('combined')); 
app.use(bodyParser.json({type: '*/*' })); 
router(app); 

// Server Setup 
const port = process.env.PORT || 3090; 
const server = http.createServer(app); 
server.listen(port); 

console.log('Server listening on:', port); 

這是日誌的mongod:

2016-11-20T16:33:13.095-0700 I CONTROL [initandlisten] MongoDB starting : pid=15054 port=27017 dbpath=/data/db 64-bit host=Bens-MacBook-Pro.local 

......

2016-11-20T16:33:13.380-0700 I NETWORK [initandlisten] waiting for connections on port 27017 

缺少什麼?

謝謝!

修訂

http://code.runnable.com/UWxv-JS8trEHAACH/connect-to-mongodb-using-mongoosejs-for-node-js

我添加了一個監聽器來檢查連接:

/* 
* More details here http://mongoosejs.com/docs/index.html 
*/ 

//require mongoose node module 
var mongoose = require('mongoose'); 

//connect to local mongodb database 
var db = mongoose.connect('mongodb://127.0.0.1:27017/test'); 

//attach lister to connected event 
mongoose.connection.once('connected', function() { 
    console.log("Connected to database") 
}); 

而且它記錄 「連接到數據庫」,所以似乎是甚至認爲蒙古窗口說它不是。讓我通讀本教程的其餘部分,看看它是否真正連接。

回答

1

你做一些錯誤

  • mongoose.connect('mongodb://localhost:auth/auth');正在尋找一個端口:auth不存在
  • const server = http.createServer(app);你可以簡單地做app.listen(port);
  • router(app);,你可以簡單地做require('yourRouteFile.js')(app);

你應該得到它與以下個server.js

// Main starting point of the application 
const express = require('express'); 
const http  = require('http'); 
const bodyParser = require('body-parser'); 
const app  = express(); 
const morgan  = require('morgan'); 
const mongoose = require('mongoose'); 
const port  = process.env.PORT || 3090; 

// DB Setup 
mongoose.connect('mongodb://localhost/auth'); 

// App Setup 
app.use(morgan('combined')); 
app.use(bodyParser.json({type: '*/*' })); 

//Routes 
require('yourRouteFile.js')(app); 

// Server Setup 
app.listen(port); 

console.log('Server listening on:', port); 
+0

@ AndrewM16。感謝您回覆。我將它改爲「mongoose.connect('mongodb:// localhost/auth');」它仍然沒有連接。 –

0

確定。所以我遵循節點+貓鼬教程的其餘部分,並使用郵遞員,我能夠保存東西到數據庫,由Robomongo驗證。但是我與mongod的日誌仍然說「等待27017端口上的連接」。所以我不知道爲什麼.....但事情的工作。