我正忙于创建Express应用并使用mongodb。
虽然我仍在学习,但在开发中,我想在docker内部使用mongodb。目前,我的进程是(在Ubuntu 18上运行)-
docker pull mongo
一旦拉开,我就用它运行
docker run -p 27017:27017 mongo
一旦运行,我可以在终端中看到mongo输出,如果我进入浏览器的端口,则会收到以下消息
It looks like you are trying to access MongoDB over HTTP on the native driver port.
然后让我的快速应用程序句柄连接到数据库,并启动服务器。
var db
var url = 'mongod://localhost:27017/'
MongoClient.connect(url,{ useNewUrlParser: true }, (err, database) => {
if(err) return console.log(err)
db = database.db('quotes_db')
app.listen(3000, () => {
console.log("listening on port 3000...");
});
})
启动快递服务器时出现错误-
{ MongoParseError: Invalid connection string
at parseConnectionString (/home/kyle/learning/web-backend/crud_tutorial/node_modules/mongodb-core/lib/uri_parser.js:429:21)
at connect (/home/kyle/learning/web-backend/crud_tutorial/node_modules/mongodb/lib/operations/mongo_client_ops.js:180:3)
at connectOp (/home/kyle/learning/web-backend/crud_tutorial/node_modules/mongodb/lib/operations/mongo_client_ops.js:284:3)
at executeOperation (/home/kyle/learning/web-backend/crud_tutorial/node_modules/mongodb/lib/utils.js:420:24)
at MongoClient.connect (/home/kyle/learning/web-backend/crud_tutorial/node_modules/mongodb/lib/mongo_client.js:168:10)
at Function.MongoClient.connect (/home/kyle/learning/web-backend/crud_tutorial/node_modules/mongodb/lib/mongo_client.js:372:22)
at Object.<anonymous> (/home/kyle/learning/web-backend/crud_tutorial/server.js:10:13)
at Module._compile (internal/modules/cjs/loader.js:688:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:699:10)
at Module.load (internal/modules/cjs/loader.js:598:32)
name: 'MongoParseError',
[Symbol(mongoErrorContextSymbol)]: {} }
我知道这与连接字符串有关,但是我发现的帖子和文章都在说我做对了。
这是源代码的链接-