Walter.Bishop21
Walter.Bishop21

Reputation: 11

"Failed to load resource: the server responded with a status of 404" express docker

I got error message in browser "Failed to load resource: the server responded with a status of 404" and I am using Node/Express, Mongo Database and Docker. Everything works fine without a Docker.

Folder structure:



server.js

app.use('/', express.static(__dirname + '../../dist/'));

app.get('/gallery', (req, res) => {
  Gallery.find({}, {name: 1, path: 1, _id: 0, image: 1}).then(
    gallery => {
      res.json({gallery});
    },
    e => {
      res
        .status(500)
        .send(e)
        .send('Undefined error');
    },
  );
});

index.html

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">

    <title>Gallery</title>
    <base href="/">
    <link href="https://fonts.googleapis.com/css?family=Roboto:400,500,700" rel="stylesheet">


</head>

<body>
    <div id="root"></div>




    <script type="text/javascript" src="dist/frontend-output.js"></script>
</body>

</html>
webpack.config.js

const frontend = {
  resolve: {
    modules: [path.resolve(__dirname, 'frontend'), 'node_modules'],
  },
  mode: 'development',
  entry: './frontend/js/index.js',

  output: {
    path: path.resolve(__dirname, '/dist/'),
    publicPath: '/',
    filename: 'frontend-output.js',
  },

  module: {
    rules: [
      {
        test: /\.(js)$/,
        exclude: /node_modules/,
        use: {
          loader: 'babel-loader',
          options: {
            presets: ['env', 'stage-0', 'react'],
          },
        },
      },
      {test: /\.css$/, use: ['style-loader', 'css-loader']},
      {test: /\.(jpe?g|png|gif|svg)$/i, use: ['url-loader', 'file-loader']},
      {
        test: /\.(jpe?g|png|ttf|eot|gif|svg|woff(2)?)(\?[a-z0-9=&.]+)?$/,
        use: 'base64-inline-loader?limit=1000&name=[name].[ext]',
      },
    ],
  },

  plugins: [
    new HtmlWebpackPlugin({
      publicPath: '/',
      template: './frontend/src/index.html',
    }),
  ],
  devServer: {
    historyApiFallback: true,
  },
};

package.json {scripts}

"server": "node ./backend/server/server.js",
"webpack": " webpack-dev-server   --host 0.0.0.0  --disable-host-check",
"start": "run-p webpack server " /* run-p is part of npm-run-all package*/

Dockerfile

FROM node:latest
WORKDIR /app
COPY . /app
EXPOSE 3000

docker-compose.yml

version: "3"
services:
  app:
    container_name: projectX
    image: docker-node-express-mongoapp
    restart: always
    build: .
    command: npm run start
    ports:
      - "3000:3000"


  mongo:
    container_name: mongo
    image: mongo
    command: ["mongod", "--bind_ip_all"]
    volumes: 
       - /app:/data/configdb
       - mongo-data:/data/db
    ports:
       - "27017:27017"

volumes:
    mongo-data:

If I type in the browser "192.168.99.100:3000/gallery" I had only data (json) response from database but not any html/css code.

When I try manually send index.html to the browser with:

app.get('/', function(request, response) {
    response.sendFile(path.resolve(__dirname + '../../../frontend/src/index.html'),
  );
});

I have problem with load frontend-output.js. (404 not found)

Thank you very much for help :)

Upvotes: 1

Views: 5001

Answers (1)

Walter.Bishop21
Walter.Bishop21

Reputation: 11

PROBLEM SOLVED !!! to dockerfile add:

  • EXPOSE 3000
  • EXPOSE 8080

to docker-compose.yml add:

  • ports:
  • "8080:8080"
  • "3000:3000"

and set port for devServer in webpack.config.js

  • port: 8080

Upvotes: 0

Related Questions