我在运行mongod时收到此消息。
在我正在做的教程中,我想我应该得到类似“从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
这些是我的Node脚本:
// 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")
});
它正在记录“已连接到数据库”,因此即使mongod窗口显示它不是,它也会显示 。让我通过本教程的其余部分,看看它是否真正连接。
答案 0 :(得分: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);
答案 1 :(得分:0)
确定。所以我按照节点+ mongoose教程的其余部分并使用邮递员,我能够将内容保存到数据库,由Robomongo验证。但我与mongod的日志仍然说“等待端口27017上的连接”。所以我不确定为什么.....但事情有效。