I'm new in nodeJS and mongodb. I learned all these stuffs from YouTube. I tried to create an api using node.js but now I can't even connect to the mongodb.
The problem is I uses Fn project(runtime node) and tried to connect to the mongodb but it didn't work.(The Fn project is an open-source container-native serverless platform.)
This is the part that I use to connect to 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;
})
So I run the command to start the Fn Server
fn start
and I also run the command to start mongodb
sudo mongod
After everything is set, I run the command to execute the code before deploying.
sudo fn run
and this is the resule that I got:
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)
My docker version is Docker version 18.06.1-ce and Ubuntu version is Ubuntu 18.04.1 LTS.
In addition, this is the package.json and 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
I'm stuck at this error for a week and I am so lost right now.