node.jsmongodbfiwarefiware-orion

Context provider application: MongoDB connection unsuccessful


I'm trying to follow the context provider tutorial but it seems the connection from the node.js app of the context provider to mongodb is refused.

I have the next docker compose YAML file with the services just copied from the tutorials:

version: "3.8"
services:
  mongo-db:
    image: mongo:4.2
    hostname: mongo-db
    container_name: db-mongo
    expose:
        - "27017"
    ports:
        - "27017:27017"
    networks:
        - default

  orion:
    image: quay.io/fiware/orion:latest
    hostname: orion
    container_name: fiware-orion
    depends_on:
        - mongo-db
    networks:
        - default
    expose:
        - "1026"
    ports:
        - "1026:1026"
    command: -dbhost mongo-db -logLevel DEBUG

  tutorial:
    image: quay.io/fiware/tutorials.context-provider
    hostname: context-provider
    container_name: fiware-tutorial
    networks:
        - default
    expose:
        - "3000"
    ports:
        - "3000:3000"
    environment:
        - "DEBUG=tutorial:*"
        - "PORT=3000"
        - "CONTEXT_BROKER=http://orion:1026/v2"
        - "OPENWEATHERMAP_KEY_ID=<ADD_YOUR_KEY_ID>"
        - "TWITTER_CONSUMER_KEY=<ADD_YOUR_CONSUMER_KEY>"
        - "TWITTER_CONSUMER_SECRET=<ADD_YOUR_CONSUMER_SECRET>"

The project runs all the containers without problem. The first time I try to open the link http://localhost:3000/app/monitor, it seems to work. The browser shows the register of a health check I do: monitor app working

As you could see, no icons and style are applied, and when I browse again any of the health API endpoints or if I try to reload the page, it fails and shows: ECONNREFUSED

Looking into the logs of the context provider container it seems the connection to mongo is refused but no type of error is marked. This is the log for the context provider container when it starts (You could see the connection refused and how some GET actions failed after the first successfully healty check call):

2023-07-22T15:55:32.806Z tutorial:ngsi-v2 Store is retrieved using NGSI-v2
2023-07-22T15:55:33.184Z tutorial:ngsi-v1 NGSI-v1 Context-Forwarding Support is available
2023-07-22T15:55:33.185Z tutorial:ngsi-v2 NGSI-v2 Context-Forwarding Support is available
2023-07-22T15:55:48.276Z tutorial:iot-device Listening on HTTP endpoints: /iot/bell, /iot/door, iot/lamp
(node:1) [MONGOOSE] DeprecationWarning: Mongoose: the `strictQuery` option will be switched back to `false` by default in Mongoose 7. Use `mongoose.set('strictQuery', false);` if you want to prepare for this change. Or use `mongoose.set('strictQuery', true);` to suppress this warning.
(Use `node --trace-deprecation ...` to show where the warning was created)
2023-07-22T15:55:48.278Z tutorial:server Listening on port 3000
2023-07-22T15:55:48.279Z tutorial:server Listening on port 3001
2023-07-22T15:56:03.175Z tutorial:application MongoDB connection unsuccessful: {"message":"connect ECONNREFUSED 127.0.0.1:27017","reason":{"type":"Unknown","servers":{},"stale":false,"compatible":true,"heartbeatFrequencyMS":10000,"localThresholdMS":15,"setName":null,"maxElectionId":null,"maxSetVersion":null,"commonWireVersion":0,"logicalSessionTimeoutMinutes":null}}
2023-07-22T15:56:03.175Z tutorial:application retry after 5 seconds.
Error [ERR_HTTP_HEADERS_SENT]: Cannot set headers after they are sent to the client
    at new NodeError (node:internal/errors:387:5)
    at ServerResponse.setHeader (node:_http_outgoing:644:11)
    at ServerResponse.header (/opt/tutorial/node_modules/express/lib/response.js:776:10)
    at ServerResponse.send (/opt/tutorial/node_modules/express/lib/response.js:170:12)
    at done (/opt/tutorial/node_modules/express/lib/response.js:1013:10)
    at Object.exports.renderFile (/opt/tutorial/node_modules/pug/lib/index.js:448:12)
    at View.exports.__express [as engine] (/opt/tutorial/node_modules/pug/lib/index.js:493:11)
    at View.render (/opt/tutorial/node_modules/express/lib/view.js:135:8)
    at tryRender (/opt/tutorial/node_modules/express/lib/application.js:640:10)
    at Function.render (/opt/tutorial/node_modules/express/lib/application.js:592:3)
    at ServerResponse.render (/opt/tutorial/node_modules/express/lib/response.js:1017:7)
    at /opt/tutorial/app.js:124:9
    at Layer.handle_error (/opt/tutorial/node_modules/express/lib/router/layer.js:71:5)
    at trim_prefix (/opt/tutorial/node_modules/express/lib/router/index.js:321:13)
    at /opt/tutorial/node_modules/express/lib/router/index.js:284:7
    at Function.process_params (/opt/tutorial/node_modules/express/lib/router/index.js:341:12)
    at next (/opt/tutorial/node_modules/express/lib/router/index.js:275:10)
    at Layer.handle_error (/opt/tutorial/node_modules/express/lib/router/layer.js:67:12)
    at trim_prefix (/opt/tutorial/node_modules/express/lib/router/index.js:321:13)
    at /opt/tutorial/node_modules/express/lib/router/index.js:284:7
    at Function.process_params (/opt/tutorial/node_modules/express/lib/router/index.js:341:12)
    at Immediate.next (/opt/tutorial/node_modules/express/lib/router/index.js:275:10)
Error [ERR_HTTP_HEADERS_SENT]: Cannot set headers after they are sent to the client
    at new NodeError (node:internal/errors:387:5)
    at ServerResponse.setHeader (node:_http_outgoing:644:11)
    at ServerResponse.header (/opt/tutorial/node_modules/express/lib/response.js:776:10)
    at ServerResponse.send (/opt/tutorial/node_modules/express/lib/response.js:170:12)
    at done (/opt/tutorial/node_modules/express/lib/response.js:1013:10)
    at Object.exports.renderFile (/opt/tutorial/node_modules/pug/lib/index.js:448:12)
    at View.exports.__express [as engine] (/opt/tutorial/node_modules/pug/lib/index.js:493:11)
    at View.render (/opt/tutorial/node_modules/express/lib/view.js:135:8)
    at tryRender (/opt/tutorial/node_modules/express/lib/application.js:640:10)
    at Function.render (/opt/tutorial/node_modules/express/lib/application.js:592:3)
    at ServerResponse.render (/opt/tutorial/node_modules/express/lib/response.js:1017:7)
    at /opt/tutorial/app.js:124:9
    at Layer.handle_error (/opt/tutorial/node_modules/express/lib/router/layer.js:71:5)
    at trim_prefix (/opt/tutorial/node_modules/express/lib/router/index.js:321:13)
    at /opt/tutorial/node_modules/express/lib/router/index.js:284:7
    at Function.process_params (/opt/tutorial/node_modules/express/lib/router/index.js:341:12)
    at next (/opt/tutorial/node_modules/express/lib/router/index.js:275:10)
    at Layer.handle_error (/opt/tutorial/node_modules/express/lib/router/layer.js:67:12)
    at trim_prefix (/opt/tutorial/node_modules/express/lib/router/index.js:321:13)
    at /opt/tutorial/node_modules/express/lib/router/index.js:284:7
    at Function.process_params (/opt/tutorial/node_modules/express/lib/router/index.js:341:12)
    at Immediate.next (/opt/tutorial/node_modules/express/lib/router/index.js:275:10)
2023-07-22T15:56:38.190Z tutorial:application MongoDB connection unsuccessful: {"message":"connect ECONNREFUSED 127.0.0.1:27017","reason":{"type":"Unknown","servers":{},"stale":false,"compatible":true,"heartbeatFrequencyMS":10000,"localThresholdMS":15,"setName":null,"maxElectionId":null,"maxSetVersion":null,"commonWireVersion":0,"logicalSessionTimeoutMinutes":null}}
2023-07-22T15:56:38.190Z tutorial:application retry after 5 seconds.
Error [ERR_HTTP_HEADERS_SENT]: Cannot set headers after they are sent to the client
    at new NodeError (node:internal/errors:387:5)
    at ServerResponse.setHeader (node:_http_outgoing:644:11)
    at ServerResponse.header (/opt/tutorial/node_modules/express/lib/response.js:776:10)
    at ServerResponse.send (/opt/tutorial/node_modules/express/lib/response.js:170:12)
    at done (/opt/tutorial/node_modules/express/lib/response.js:1013:10)
    at Object.exports.renderFile (/opt/tutorial/node_modules/pug/lib/index.js:448:12)
    at View.exports.__express [as engine] (/opt/tutorial/node_modules/pug/lib/index.js:493:11)
    at View.render (/opt/tutorial/node_modules/express/lib/view.js:135:8)
    at tryRender (/opt/tutorial/node_modules/express/lib/application.js:640:10)
GET /version 200 5.371 ms - 21
GET /version 200 2.035 ms - 21
GET /app/monitor 200 32.774 ms - 2038
GET /stylesheets/style.css 500 1.921 ms - 562
GET /img/fiware.svg 500 1.490 ms - 562
GET /img/favicon.ico 500 0.795 ms - 562
GET /version 200 1.036 ms - 21
GET /health/static 200 3.063 ms - 103
GET /health/random 500 0.649 ms - 562
GET /health/static 500 0.589 ms - 562
GET /version 200 1.242 ms - 21
GET /version 200 0.702 ms - 21
GET /version 200 1.410 ms - 21
    at Function.render (/opt/tutorial/node_modules/express/lib/application.js:592:3)
    at ServerResponse.render (/opt/tutorial/node_modules/express/lib/response.js:1017:7)
    at /opt/tutorial/app.js:124:9
    at Layer.handle_error (/opt/tutorial/node_modules/express/lib/router/layer.js:71:5)
    at trim_prefix (/opt/tutorial/node_modules/express/lib/router/index.js:321:13)
    at /opt/tutorial/node_modules/express/lib/router/index.js:284:7
    at Function.process_params (/opt/tutorial/node_modules/express/lib/router/index.js:341:12)
    at next (/opt/tutorial/node_modules/express/lib/router/index.js:275:10)
    at Layer.handle_error (/opt/tutorial/node_modules/express/lib/router/layer.js:67:12)
    at trim_prefix (/opt/tutorial/node_modules/express/lib/router/index.js:321:13)
    at /opt/tutorial/node_modules/express/lib/router/index.js:284:7
    at Function.process_params (/opt/tutorial/node_modules/express/lib/router/index.js:341:12)
    at Immediate.next (/opt/tutorial/node_modules/express/lib/router/index.js:275:10)
Error [ERR_HTTP_HEADERS_SENT]: Cannot set headers after they are sent to the client
    at new NodeError (node:internal/errors:387:5)
    at ServerResponse.setHeader (node:_http_outgoing:644:11)
    at ServerResponse.header (/opt/tutorial/node_modules/express/lib/response.js:776:10)
    at ServerResponse.send (/opt/tutorial/node_modules/express/lib/response.js:170:12)
    at done (/opt/tutorial/node_modules/express/lib/response.js:1013:10)
    at Object.exports.renderFile (/opt/tutorial/node_modules/pug/lib/index.js:448:12)
    at View.exports.__express [as engine] (/opt/tutorial/node_modules/pug/lib/index.js:493:11)
    at View.render (/opt/tutorial/node_modules/express/lib/view.js:135:8)
    at tryRender (/opt/tutorial/node_modules/express/lib/application.js:640:10)
    at Function.render (/opt/tutorial/node_modules/express/lib/application.js:592:3)
    at ServerResponse.render (/opt/tutorial/node_modules/express/lib/response.js:1017:7)
    at /opt/tutorial/app.js:124:9
    at Layer.handle_error (/opt/tutorial/node_modules/express/lib/router/layer.js:71:5)
    at trim_prefix (/opt/tutorial/node_modules/express/lib/router/index.js:321:13)
    at /opt/tutorial/node_modules/express/lib/router/index.js:284:7
    at Function.process_params (/opt/tutorial/node_modules/express/lib/router/index.js:341:12)
    at next (/opt/tutorial/node_modules/express/lib/router/index.js:275:10)
    at Layer.handle_error (/opt/tutorial/node_modules/express/lib/router/layer.js:67:12)
    at trim_prefix (/opt/tutorial/node_modules/express/lib/router/index.js:321:13)
    at /opt/tutorial/node_modules/express/lib/router/index.js:284:7
    at Function.process_params (/opt/tutorial/node_modules/express/lib/router/index.js:341:12)
    at Immediate.next (/opt/tutorial/node_modules/express/lib/router/index.js:275:10)

Could anyone explain me why the connection is refused and the node.js app is not working? The mongo-db container is working properly with orion broker and I can create entities and subscriptions.


Solution

  • Finally, I found a version of a docker-compose YAML in the github repository that seems to be a newer version of what it's stated on the tutorial: updated docker-compose file in github repository

    They problem was that the contex provider service needs an new environment variable to address mongo url: "MONGO_URL=mongodb://mongo-db:27017"

    Just adding it to my original docker-compose file, the application worked!

    Hope this will help anyone in the future or fiware developer could updated the tutorial with it.