1

With the below setup, I am able to see my website but I am getting an ERR_EMPTY_RESPONSE when I try to register.

When I register I am making a HTTP request to the API.js which creates/updates the Schema in user.js.

This normally works when not using Docker/Nginx. I tried connecting to the container Image "mongo" directly, I tried 0.0.0.0:27017 , but I have the feeling my app.js doesn't initially connect to the Mongodb.

Even though the routing to /main /profile etc. works. So I am assuming app.js has been called via index.html

I have a feeling it is a minor thing to make this work, but spent a few hours now and I struggle. I am not using CORS, since I am using Nginx. I am quite new to these concepts, but I think Nginx as a reverse proxy doesn't use CORS?

In docker-compose.yml when I change the directory of the Volume to something that doesn't exist, nothing changes I still get to see my website, as if it doesn't do anything, however when I delete the sentence it gives me a failure so it is being called. Not entirely sure what the Volume does in this file.

docker ps gives me the below output:

 CONTAINER    ID             IMAGE  COMMAND           CREATED           STATUS              PORTS             NAMES

 f58507ed0205 meanchat_web   "nginx -g 'daemon ..."   55 seconds ago      Up 54 seconds       0.0.0.0:80->80/tcp               meanchat_web_1

 cddfd8180f1b        meanchat_node       "nginx -g 'daemon ..."   2 hours ago         Up 55 seconds       80/tcp, 0.0.0.0:3000->3000/tcp   meanchat_node_1

 79a013dd4e51        mongo               "docker-entrypoint..."   2 hours ago         Up 55 seconds       0.0.0.0:27017->27017/tcp         meanchat_mongo_1

app.js:

 var mongo= require('mongodb');

//var cors = require('cors');
 var mongoose = require('mongoose');

 var connect = require('./models/user')
 connect.connect('mongodb://localhost/loginapp');
 var db = mongoose.connection;

user.js:

 module.exports.connect = function(mongoUri, promiseLib){

 var mongoDB = mongoose.connect(mongoUri);
  return mongoDB
  };

DockerFile:

 ### STAGE 1: Build ###

 # We label our stage as ‘builder’
 FROM node:8.1.4-alpine as builder

 COPY package.json package-lock.json ./

 ## Storing node modules on a separate layer will prevent unnecessary 
 pm installs at each build
 RUN npm i && mkdir /ng-app && cp -R ./node_modules ./ng-app

 WORKDIR /ng-app

 COPY . .

 ## Build the angular app in production mode and store the artifacts
 in dist folder
 RUN $(npm bin)/ng build --prod
 ### STAGE 2: Setup ###

 FROM nginx:1.13.3-alpine

 ## Copy our default nginx config
 COPY nginx/default.conf /etc/nginx/conf.d/

 ## Remove default nginx website
 RUN rm -rf /usr/share/nginx/html/*

 ## From ‘builder’ stage copy over the artifacts in dist folder to default
 nginx public folder
 COPY --from=builder ./dist /usr/share/nginx/html

 CMD ["nginx", "-g", "daemon off;"]

docker-compose.yml:

 version: '3'
 services:
 web:
  build: ./
  ports:
  - "80:80"
  links:
  - node
  volumes:
   - "./dist :/usr/share/nginx/html"
 node:
  build: ./
  ports:
  - "3000:3000"
   links:
   - mongo
 mongo:
 image: mongo
 ports: 
 - "27017:27017"

main.ts:

enter image description here

3 Answers 3

1

I can see from the compose output that mongo wasn't connected proper in your app.js.

Change this line connect.connect('mongodb://localhost/loginapp'); to

connect.connect('mongodb://mongo/loginapp');

then run docker-compose build and docker-compose up and it should connect.

Sign up to request clarification or add additional context in comments.

4 Comments

I tried the above, but it doesn't work, I added a usera ccount to get rid of the user allowed access problem, which is now gone, however I am still getting the err-empty message when trying to do a get to the mongodb server. Is there a way to switch off the access control on mongodb or what else could be the reason the mongoose doesn't work on the mongodb. Whilst it is logging in on the mongodb.
can you give a stack trace? It's really hard to help without seeing the error.
Hi Mike. I will try and figure out how to get the stacktrace, the warnings I get from the browser console generally give very little info when it comes down to http calls. Is there a way to get more stacktrace info from the server api.js? For instance when I put a console.log in the method that is being called on the api I see no log at all. I am not great at debugging as you may see 🤔
Failed to load resource: net::ERR_EMPTY_RESPONSE main.1562976….bundle.js:1 t error : ProgressEvent {isTrusted: true, lengthComputable: false, loaded: 0, total: 0, type: "error", …} headers : e {normalizedNames: Map(0), lazyUpdate: null, headers: Map(0)} message : "Http failure response for (unknown url): 0 Unknown Error" name : "HttpErrorResponse" ok : false status : 0 statusText : "Unknown Error" url : null proto : Object
0

The image mongo is not connected.

You should use

connect.connect('mongodb:27017//localhost/db'); 

to connect to the db.

1 Comment

Hi Roman, just noticed in the output it alks about dbpath = data/db, I thought u meant by db the database name loginapp. So in my case I created a loginapp(use loginapp) would the correct line become: connect.connect(‘mongodb:27017//localhost/db/loginapp’)?
0

I tried both of your options, but the result seems the same, I sent the output again. I did do docker-compose build as you mentioned and docker-compose up.

I am doing the following get to the api in order to register to the mongodatabase.

 let url = 'http://localhost:3000/api/register';
 return this.http.post(url, user)

I am able to connect to the mongodb in the container by doing mongo://0.0.0.0:27017 from the D:/mongodb/bin folder. It does give me that warning: Access control is not enabled for the database.

I turned the firewall off and no antivir is active atm.

I googled the Access Control warning I am getting and this seems to suggest I need create an authorization account on mongodb? Is that required even when using Mongoose, without docker/nginx I didn't have this issue. Perhaps it has something to do with the image of mongodb?

I added an admin account by logging into the database: $ use loginapp $ db.createUser( { user: "username", pwd: "password", roles: [ { role: "root", db: "admin" } ] })

mongo:
  environment:
  - MONGO_INITDB_ROOT_USERNAME=username
  - MONGO_INITDB_ROOT_PASSWORD=password
image: mongo
ports: 
- "27017:27017"

now I am able to connect to the database without the acces control is not enabled message however when I try to register I am still getting the same ERR_empty_response. So is there a way to get rid of the access control entirely or do I have to add something to the schema of mongoose so that when I try to change or create a user on LoginApp it is authenticating properly?

newUser.save forinstance probably won't work now because it requires separate authentication?

Below picture is what I am getting now, however the get to the mongoose database still doesn't work.

enter image description here

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.