我正在 Kubernetes 上使用 React 和 Node.js 构建 SPA。我为前端和后端服务提供了单独的服务和入口。我见过人们也使用 Nginx 来服务 React 构建,但我发现下面的做法效果很好。
# Dockerfile.production
FROM node:8.7.0-alpine
RUN mkdir -p /usr/app/client
WORKDIR /usr/app/client
COPY package*.json /usr/app/client/
RUN npm install
RUN npm install -g serve
COPY . /usr/app/client
EXPOSE 3000
RUN npm run build
CMD ["serve", "-s", "build", "-l", "3000" ]
Run Code Online (Sandbox Code Playgroud)
或者,我可以使用 Nginx 来提供构建服务,如下所示。这似乎是“正确的方法”,但我不确定与使用serve npm 包相比有什么优势,尽管它对我来说确实感觉很hacky。似乎可以使用 Nginx 配置来为应用程序提供服务的所有内容也可以在 Ingress 中完成,对吗?
server {
server_name example.com;
...
location ~ / {
root /var/www/example.com/static;
try_files $uri /index.html;
}
}
Run Code Online (Sandbox Code Playgroud)