首页 > 解决方案 > Express 服务器作为 Docker 镜像容器运行,只为一些 html 页面提供服务

问题描述

问题

Node/express Docker 映像在本地运行,服务于一些 html 页面而不是其他页面

解释

我有一个 Node/Express 应用程序作为 Docker 映像,作为 Docker 容器在本地运行。这是一个应用程序在使用 Express 时以通常的方式提供 html 页面。但是,尽管快速路线几乎相同,但似乎我只能访问其中一些页面,而不能访问其他页面

server.js

app.get('/', function (req, res) {
  res.render('index', {
    user: req.user
  });
});

app.get('/signup/terms', function (req, res, ) {
  res.render('signup/terms', {
    user: req.user
  })
})

app.get('/signup/map', function (req, res, ) {
  res.render('signup/map', {
    user: req.user
  })
})

我可以访问 index 和 /signup/terms 没问题,加载所有 html、css 和图像,但是当我访问 /signup/map 时,我得到一个 500 状态码

GET /signup/map 500 5.980 ms - 47

我尝试过的事情:

我尝试使用以下命令查看 docker 容器文件目录:

docker exec -it <container> sh

检查所有文件是否存在,并且图像是最新的。

这是我用来构建图像的 docker 配置:

FROM node:10-alpine

# Create app directory
WORKDIR /app

# Install app dependencies
# A wildcard is used to ensure both package.json AND package-lock.json are copied
# where available (npm@5+)
COPY package*.json ./

# DEV RUN npm install
# If you are building your code for production
# RUN npm ci --only=production
RUN npm ci --only=production

# Bundle app source
COPY . /app

EXPOSE 3000
CMD [ "node", "server.js" ]

我也尝试使用 --no-cache 选项重建图像,但无济于事。

我认为这可能是一个目录问题,但有些页面正在服务而有些没有这一事实令人困惑......

有谁知道这里发生了什么?

更新:原来这是 ejs 模板的问题。我的页面有未传递的变量,导致服务器抛出 500 状态。

标签: node.jsdockerexpress

解决方案


推荐阅读