“无法加载资源:服务器响应状态为404” express docker

时间:2018-08-21 15:46:46

标签: javascript node.js express docker webpack

我在浏览器中收到错误消息“无法加载资源:服务器响应状态为404”,并且我正在使用Node / Express,Mongo Database和Docker。 没有Docker,一切都可以正常工作。

文件夹结构:


  • 后端
    • 服务器
      • server.js
  • 前端
    • src
      • index.html
  • dist
  • webpack.config.js
  • package.json
  • Dockerfile
  • docker-compose.yml

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 {脚本}

"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:

如果我在浏览器中输入“ 192.168.99.100:3000/gallery”,则我只有来自数据库的数据(json)响应,而没有任何html / css代码。

当我尝试通过以下方式手动将index.html发送到浏览器时:

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

我对load frontend-output.js有问题。 (找不到404)

非常感谢您的帮助:)

1 个答案:

答案 0 :(得分:0)

问题解决了!!! 到dockerfile添加:

  • EXPOSE 3000
  • 博览会8080

向docker-compose.yml添加:

  • 端口:
  • “ 8080:8080”
  • “ 3000:3000”

并在webpack.config.js中设置devServer的端口

  • 端口:8080