Node.js 在docker中提供bundle.js
一段时间以来,我一直在尝试将应用程序部署到aws。我的web应用在本地运行得非常好,但当它通过docker容器托管在aws上时,我遇到了以下错误:Node.js 在docker中提供bundle.js,node.js,amazon-web-services,docker,webpack,Node.js,Amazon Web Services,Docker,Webpack,一段时间以来,我一直在尝试将应用程序部署到aws。我的web应用在本地运行得非常好,但当它通过docker容器托管在aws上时,我遇到了以下错误: Failed to load resource: the server responded with a status of 404 (bundle.js) or... GET http://my_AWS_ROUTE_53/bundle.js net::ERR_ABORTED 文件夹结构: config --> webpack.config.
Failed to load resource: the server responded with a status of 404 (bundle.js)
or...
GET http://my_AWS_ROUTE_53/bundle.js net::ERR_ABORTED
文件夹结构:
config
--> webpack.config.js
dist
--> bundle.js
src
--> public (where all of my client code is, also what is producing bundle.js)
--> index.js
--> server
--> static
--> index.html
Dockerfile
app.js
下面是我在app.js中为文件提供服务的方式:
app.use('/', express.static(__dirname + '/src/server/static/'));
app.use('/', express.static(__dirname + '/dist/'));
app.get('/', function (request, response){
response.sendFile(__dirname + '/src/server/static/index.html');
})
用于输出的webpack.config.js:
entry: path.resolve(__dirname, "../src/public/index.js"),
output: {
path: path.resolve(__dirname, '../dist/'),
publicPath: "/",
filename: "bundle.js"
},
Index.html:
<html>
<head>
<meta charset="utf-8">
<base href="/" />
<title>Test</title>
</head>
<body>
<div id="myTestApp"> </div>
</body>
<script src="bundle.js"></script>
</html>
npm开始做什么?包括运行网页包吗您可能希望在为资产提供服务(即启动服务器)之前运行构建,npm start=“node app.js”将启动我的express后端服务器。所以不,它不包括运行网页包。但是,bundle.js仍在dist文件夹中,docker在复制中的所有内容时应该可以访问它。您只复制了package.json,容器中将不包括do local builds(dist/)文件夹。-因此,您需要添加一个命令,在dockerfile中构建
webpack
,以便找到并提供bundle.js。-将其包含在dockerfile中,这将得到解决。ADD/var/www/app/current
将所有内容从根目录复制到/var/www/app/current,包括dist目录。我的.dockerginore
文件中唯一的一项是node_modules文件夹。docker exec bash
放入容器中,并ls
放入该文件夹(确认)-(如果是这样,我有点困惑,将按照下面的答案回答)npm启动做什么?包括运行webpack吗?-您可能希望在为资产提供服务(即启动服务器)之前运行构建,npm start=“node app.js”将启动我的express后端服务器。所以不,它不包括运行网页包。但是,bundle.js仍在dist文件夹中,docker在复制中的所有内容时应该可以访问它。您只复制了package.json,容器中将不包括do local builds(dist/)文件夹。-因此,您需要添加一个命令,在dockerfile中构建webpack
,以便找到并提供bundle.js。-将其包含在dockerfile中,这将得到解决。ADD/var/www/app/current
将所有内容从根目录复制到/var/www/app/current,包括dist目录。我的.dockerginore
文件中唯一的一项是node\u modules文件夹。docker exec bash
放入容器,并ls
放入该文件夹(以确认)-(如果是这样,我有点不知所措,将跟随回答)
FROM node:7.9.0
ENV appDir /var/www/app/current
RUN mkdir -p /var/www/app/current
WORKDIR ${appDir}
ADD package.json ./
RUN npm i
ADD . /var/www/app/current
EXPOSE 3001
CMD ["npm", "start"]