skip to Main Content

Here’s most up-to-date configuration since adding bounty: Configuration

UPDATE: It looks like it’s getting a cors error when clicking login, which is most likely causing the issue. I’ve read in other posts like this one that I need to update nginx.conf. I haven’t been able to figure out how to update said file since it’s already dockerized assuming this is the solution to my problem. Also could be related to not referencing cosmodb in docker-comopose, but when I add this

    cosomsdb:
        build: ./backend
        ports:
        - "10255:10255"

I get cors error still. 10255 is cosmosdb port

Cross-Origin Request Blocked: The Same Origin Policy disallows reading the remote resource at http://localhost:3000/api/user/login. (Reason: CORS request did not succeed).
2
ERROR 
Object { headers: {…}, status: 0, statusText: "Unknown Error", url: "http://localhost:3000/api/user/login", ok: false, name: "HttpErrorResponse", message: "Http failure response for http://localhost:3000/api/user/login: 0 Unknown Error", error: error }
core.js:6014:19

I’m trying to build a MEAN stack docker image, but every time I run it, it says

Error: Cannot find module '../backend/middleware/check-auth'

I don’t think it’s copying my files to the container correctly. I’ve been going through forums, but haven’t been able to find a solution. I appreciate any help!

Dockerfile

FROM node

MAINTAINER Phil

WORKDIR /src

COPY . .

RUN npm install

RUN npm install -g nodemon

EXPOSE 3000

CMD ["npm", "start"]

docker-compose.yml

version: '3'
services:
    web:
        image: nginx
        ports:
            - "80:80"
        volumes:
        - "/Users/Phil/Documents/myapp/myapp-docker/frontend/dist:/usr/share/nginx/html"
    node:
        build: ./backend
        ports:
        - "3000:3000"

backend package.json

{
  "name": "backend",
  "version": "1.0.0",
  "description": "",
  "main": "app.js",
  "scripts": {
    "test": "echo "Error: no test specified" && exit 1",
    "start": "nodemon server.js"
  },
  "author": "",
  "license": "ISC",
  "dependencies": {
    "aws-sdk": "^2.612.0",
    "bcrypt": "^3.0.8",
    "body-parser": "^1.19.0",
    "create-hash": "^1.2.0",
    "crypto": "^1.0.1",
    "dotenv": "^8.2.0",
    "express": "^4.17.1",
    "jsonwebtoken": "^8.5.1",
    "mongoose": "^5.8.11",
    "mongoose-unique-validator": "^2.0.3",
    "multer": "^1.4.2",
    "multer-s3": "^2.9.0"
  }
}

server.js file

const app = require("./app");
const debug = require("debug")("node-angular");
const http = require("http");
const mongoose = require("mongoose");
var redis = require("redis");

var env = require("dotenv").config();


const normalizePort = val => {
  var port = parseInt(val, 10);

  if (isNaN(port)) {
    e;
    // named pipe
    return val;
  }

  if (port >= 0) {
    // port number
    return port;
  }

  return false;
};

const onError = error => {
  if (error.syscall !== "listen") {
    throw error;
  }
  const bind = typeof port === "string" ? "pipe " + port : "port " + port;
  switch (error.code) {
    case "EACCES":
      console.error(bind + " requires elevated privileges");
      process.exit(1);
      break;
    case "EADDRINUSE":
      console.error(bind + " is already in use");
      process.exit(1);
      break;
    default:
      throw error;
  }
};
console.log("process.env.COSMODDB_USER");
console.log(env.COSMODDB_USER);
mongoose
  .connect(
    "mongodb://" +
      process.env.COSMOSDB_HOST +
      ":" +
      process.env.COSMOSDB_PORT +
      "/" +
      process.env.COSMOSDB_DBNAME +
      "?ssl=true&replicaSet=globaldb",
    {
      auth: {
        user: process.env.COSMODDB_USER,
        password: process.env.COSMOSDB_PASSWORD
      }
    }
  )
  .then(() => console.log("Connection to CosmosDB successful"))
  .catch(err => console.error(err));

const onListening = () => {
  const addr = server.address();
  const bind = typeof port === "string" ? "pipe " + port : "port " + port;
  debug("Listening on " + bind);
};

const port = normalizePort(process.env.PORT || "3000");
app.set("port", process.env.PORT || port);

var server = app.listen(app.get("port"), function() {
  debug("Express server listening on port " + server.address().port);
});


enter image description here

4

Answers


  1. If you omit the version of node in Dockerfile, latest will be used. Check the version that you are using on your local machine and specify it (also, specifying the versions in Dockerfile is a good practice)

    Login or Signup to reply.
  2. Hey Could you please check your .dockerignore file and see that you’re not omitting the files you really want in the container while building?

    Login or Signup to reply.
  3. This looks like your importing '../backend/middleware/check-auth' in app.js? If so, change you import path to './backend/middleware/check-auth'. Your compose file and container seem fine. This looks more like a path issue for your module import.

    Login or Signup to reply.
  4. For the CORS problem, try adding the lib cors in your server.js like this:

    const cors = require('cors')
    app.use(cors({
      credentials: true,
      origin: [ 'http://localhost:3000' ]
    }))
    

    Lets see if it helps.
    By the way, dotenv should always come first (;

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search