gpt4 book ai didi

node.js - node.js - 错误:第一次连接时无法连接到服务器[localhost:27017] [MongoNetworkError:connect ECONNREFUSED 127.0.0.1:27017]

转载 作者:可可西里 更新时间:2023-11-01 10:46:02 25 4
gpt4 key购买 nike

我是nodejs和mongodb的新手。我从YouTube上学到了这些东西。我试图使用node.js创建一个api,但现在连mongodb都连接不上。
问题是我使用了fn project(运行时节点),并试图连接到mongodb,但它没有工作(fn project是一个开源容器本机无服务器平台)。
这是我用来连接MongoDB的部分:

var MongoClient = require('mongodb').MongoClient;
var url = 'mongodb://localhost:27017/MyDB';

MongoClient.connect(url,function(err, db) {
if(err) throw err;
console.log("Database is Connected!");
db.close();
});

//This is the part that Fn Project generated automatically
var fdk = require('@fnproject/fdk');

fdk.handle(function(input){
var name = 'World';
if (input.name) {
name = input.name;
}
response = {'message': 'Hello ' + name}
return response;
})

所以我运行命令启动fn服务器
fn start

我还运行命令来启动MongoDB
sudo mongod

设置好所有内容后,我运行命令在部署之前执行代码。
sudo fn run

这就是我得到的结果:
Building image nodefn:0.0.3 ...
(node:1) DeprecationWarning: current URL string parser is deprecated, and will be removed in a future version. To use the new parser, pass option { useNewUrlParser: true } to MongoClient.connect.
/function/node_modules/mongodb/lib/operations/mongo_client_ops.js:466
throw err;
^

Error: failed to connect to server [localhost:27017] on first connect [MongoNetworkError: connect ECONNREFUSED 127.0.0.1:27017]
at Pool.<anonymous> (/function/node_modules/mongodb-core/lib/topologies/server.js:564:11)
at emitOne (events.js:115:13)
at Pool.emit (events.js:210:7)
at Connection.<anonymous> (/function/node_modules/mongodb-core/lib/connection/pool.js:317:12)
at Object.onceWrapper (events.js:318:30)
at emitTwo (events.js:125:13)
at Connection.emit (events.js:213:7)
at Socket.<anonymous> (/function/node_modules/mongodb-core/lib/connection/connection.js:246:50)
at Object.onceWrapper (events.js:316:30)
at emitOne (events.js:115:13)

我的docker版本是docker版本18.06.1-ce
ubuntu版本是ubuntu 18.04.1lts。
另外,这是package.json和func.yaml
{
"name": "hellofn",
"version": "1.0.0",
"description": "backend",
"main": "func.js",
"author": "",
"license": "Apache-2.0",
"dependencies": {
"@fnproject/fdk": "0.x",
"mongodb": "^3.1.4"
}
}

——
//func.yaml
schema_version: 20180708
name: nodefn
version: 0.0.3
runtime: node
entrypoint: node func.js
format: json
triggers:
- name: nodefn-trigger
type: http
source: /nodefn-trigger

这个错误让我纠结了一个星期,我现在很迷茫。

最佳答案

你需要注意两件事:
1)通过nodejs创建db时,检查mongodb是否正确安装并运行在您的机器上,记住除非您创建了一个记录,否则您将无法在mongo中实际创建db,尽管它说db created!!
如果需要,请查看此链接了解如何安装mongo:https://docs.mongodb.com/v3.0/tutorial/install-mongodb-on-os-x/
2)在使用require()导入时,检查是否在工作应用程序目录(本地)上安装了MongoDB模块。
如果是,下面的代码应该可以正常工作-不需要将本地主机更改为您的IP,除非您有任何连接问题:

var MongoClient = require('mongodb').MongoClient;
var url = "mongodb://localhost:27017/MyDB"; // mydatabase is the name of db
MongoClient.connect(url, function(err, db) {
if (err) throw err;
console.log("Database created!");
db.close();
});

关于node.js - node.js - 错误:第一次连接时无法连接到服务器[localhost:27017] [MongoNetworkError:connect ECONNREFUSED 127.0.0.1:27017],我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52234587/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com