Skip to content

Commit

Permalink
fix: nest 采用 socket.io v4 版本,生产环境下前端连接 socket.io 报错
Browse files Browse the repository at this point in the history
fix #190
  • Loading branch information
cumt-robin committed Dec 17, 2024
1 parent 1a66c0e commit 373b310
Show file tree
Hide file tree
Showing 22 changed files with 127 additions and 271 deletions.
8 changes: 8 additions & 0 deletions .changeset/funny-walls-rescue.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
---
"express-server": patch
"webpack-vue3": patch
"cra-react18": patch
"vite-vue3": patch
---

fix: nest 采用 socket.io v4 版本,生产环境下前端连接 socket.io 报错
15 changes: 6 additions & 9 deletions Dockerfile.dev
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,16 @@ FROM base AS build
COPY . /usr/src/app
WORKDIR /usr/src/app
RUN --mount=type=cache,id=pnpm,target=/pnpm/store pnpm install --frozen-lockfile
# RUN pnpm deploy --filter=vite-vue3 /app/vite-vue3
RUN pnpm deploy --filter=vite-vue3 /app/vite-vue3
# RUN pnpm deploy --filter=cra-react18 /app/cra-react18
RUN pnpm deploy --filter=express-server /app/express-server
# RUN pnpm deploy --filter=nest-server /app/nest-server

# FROM base AS vite-vue3-frontend
# COPY --from=build /app/vite-vue3 /usr/src/fullstack-blog/app/vite-vue3
# WORKDIR /usr/src/fullstack-blog/app/vite-vue3
# EXPOSE 3000
# CMD ["pnpm", "dev"]
FROM base AS vite-vue3-frontend
COPY --from=build /app/vite-vue3 /usr/src/fullstack-blog/app/vite-vue3
WORKDIR /usr/src/fullstack-blog/app/vite-vue3
EXPOSE 3000
CMD ["pnpm", "dev"]

# FROM base AS cra-react18-frontend
# COPY --from=build /app/cra-react18 /usr/src/fullstack-blog/app/cra-react18
Expand All @@ -27,9 +27,6 @@ RUN pnpm deploy --filter=express-server /app/express-server
FROM base AS express-backend
RUN npm i -g pm2-dev
COPY --from=build /app/express-server /usr/src/fullstack-blog/app/express-server
WORKDIR /usr/src/fullstack-blog/app/express-server
EXPOSE 8002
CMD ["pnpm", "docker-dev"]

# FROM base AS nestjs-backend
# COPY --from=build /app/nest-server /usr/src/fullstack-blog/app/nest-server
6 changes: 4 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

Hello,这是[Vue3+TS+Node打造个人博客](https://juejin.cn/column/7177402980180688952)开源全栈项目,最初采用 Vue3 + Vue CLI + Typescript + NodeJS Express 实现,目前已经涵盖了多种技术实现,无论你是 Webpack/Vite 使用者,或者是 Vue/React 爱好者,这里都有你想要的内容,你甚至可以从这里学会多种框架的使用。

除了最初采用的 Express,后端目前也提供了 NestJS 实现,并且后续会主要维护 NestJS 版本。

如果你是一个前端开发者,还不太清楚后端开发或者全栈的概念,请一定不要错过这个项目,我相信它会对你的工作或者求职有所帮助!

本项目最新代码采用 pnpm monorepo 架构搭建,工程化能力和开发体验都比较不错,目前支持:
Expand Down Expand Up @@ -43,8 +45,8 @@ Hello,这是[Vue3+TS+Node打造个人博客](https://juejin.cn/column/71774029

后端部分:

- [x] Express + MySQL: 见目录 app/express-server
- [ ] NestJS 开发中...
- [x] NestJS + MySQL + TypeORM + class-validator: 见目录 app/nest-server
- [x] Express + MySQL + express-validator: 见目录 app/express-server

## Features

Expand Down
2 changes: 1 addition & 1 deletion app/cra-react18/.env
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
REACT_APP_BASE_API=/api
REACT_APP_SOCKET_SERVER=http://localhost:8002
REACT_APP_SOCKET_SERVER=http://127.0.0.1:8012
2 changes: 1 addition & 1 deletion app/cra-react18/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
"react-redux": "^9.1.2",
"react-refresh": "^0.11.0",
"react-router-dom": "^6.24.1",
"socket.io-client": "^2.5.0",
"socket.io-client": "^4.8.1",
"styled-components": "^6.1.11",
"tailwindcss": "^3.4.6",
"web-vitals": "^2.1.4"
Expand Down
2 changes: 1 addition & 1 deletion app/cra-react18/src/setupProxy.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ module.exports = function (app) {
app.use(
"/api",
createProxyMiddleware({
target: "http://express-server:8002",
target: "http://127.0.0.1:8012",
changeOrigin: true,
pathRewrite: {
"^/api": "",
Expand Down
Empty file added app/express-server/.env
Empty file.
2 changes: 1 addition & 1 deletion app/express-server/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
"nodemailer": "^4.6.8",
"openai": "^3.1.0",
"serve-favicon": "~2.4.5",
"socket.io": "^2.1.1",
"socket.io": "^4.8.1",
"svg-captcha": "^1.3.12",
"xss": "^1.0.9"
},
Expand Down
28 changes: 14 additions & 14 deletions app/express-server/process-dev.json
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
{
"apps": [
{
"name": "blog",
"script": "src/app.js",
"env": {
"NODE_ENV": "development",
"PORT": 8002
},
"watch": true,
"ignore_watch": ["node_modules", "*.md"]
}
]
}
{
"apps": [
{
"name": "blog",
"script": "src/app.js",
"env": {
"NODE_ENV": "development",
"PORT": 8002
},
"watch": true,
"ignore_watch": ["node_modules", "*.md"]
}
]
}
14 changes: 13 additions & 1 deletion app/express-server/src/utils/ws.js
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,19 @@ function broadcastExceptSelf(socket, param) {
}

function startWs(server) {
io = socketIo(server);
io = socketIo(server, {
cors: {
origin: (origin, callback) => {
const list = process.env.WEB_SOCKET_WHITE_LIST.split(",");
if (list.includes(origin)) {
callback(null, true);
} else {
callback(new Error("Not allowed by CORS"));
}
},
credentials: true,
},
});
chatRoomHandler();
}

Expand Down
2 changes: 1 addition & 1 deletion app/vite-vue3/.env.development
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
# 开发环境变量
VITE_APP_SOCKET_SERVER=http://localhost:8012
VITE_APP_SOCKET_SERVER=http://127.0.0.1:8012
2 changes: 1 addition & 1 deletion app/vite-vue3/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
"mitt": "^3.0.1",
"pinia": "^2.1.7",
"qs": "^6.12.1",
"socket.io-client": "^2.1.1",
"socket.io-client": "^4.8.1",
"vue": "^3.4.29",
"vue-router": "^4.0.16"
},
Expand Down
2 changes: 1 addition & 1 deletion app/vite-vue3/vite.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ export default defineConfig({
port: 3000,
proxy: {
"/api": {
target: "http://127.0.0.1:8012",
target: "http://127.0.0.1:8002",
changeOrigin: true,
rewrite: (path) => path.replace(/^\/api/, ""),
},
Expand Down
2 changes: 1 addition & 1 deletion app/webpack-vue3/.env.development
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
# 开发环境变量
VUE_APP_SOCKET_SERVER=http://localhost:8002
VUE_APP_SOCKET_SERVER=http://127.0.0.1:8012
2 changes: 1 addition & 1 deletion app/webpack-vue3/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
"mermaid": "^10.9.1",
"mitt": "^3.0.1",
"qs": "^6.10.1",
"socket.io-client": "^2.1.1",
"socket.io-client": "^4.8.1",
"vue": "~3.3.13",
"vue-router": "~4.0.8",
"vuex": "~4.0.1"
Expand Down
2 changes: 1 addition & 1 deletion app/webpack-vue3/vue.config.docker.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ module.exports = {
open: true,
proxy: {
"/api": {
target: "http://express-server:8002",
target: "http://nest-server:8012",
changeOrigin: true,
pathRewrite: {
"^/api": "",
Expand Down
2 changes: 1 addition & 1 deletion app/webpack-vue3/vue.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ module.exports = {
open: true,
proxy: {
"/api": {
target: "http://express-server:8002",
target: "http://127.0.0.1:8012",
changeOrigin: true,
pathRewrite: {
"^/api": "",
Expand Down
4 changes: 2 additions & 2 deletions build-dev-images.sh
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
#!/bin/bash

# docker build . -f Dockerfile.dev --target vite-vue3-frontend --tag fullstack-blog-vite-vue3:dev
docker build . -f Dockerfile.dev --target vite-vue3-frontend --tag fullstack-blog-vite-vue3:dev
# docker build . -f Dockerfile.dev --target cra-react18-frontend --tag fullstack-blog-cra-react18:dev
# docker build . -f Dockerfile.dev --target express-backend --tag fullstack-blog-express:dev
docker build . -f Dockerfile.dev --target express-backend --tag fullstack-blog-express:dev
# docker build . -f Dockerfile.dev --target nestjs-backend --tag fullstack-blog-nestjs:dev

echo "build dev images successfully."
30 changes: 17 additions & 13 deletions compose-dev.yml
Original file line number Diff line number Diff line change
Expand Up @@ -34,19 +34,20 @@ services:
# - CHOKIDAR_USEPOLLING=true
# - DANGEROUSLY_DISABLE_HOST_CHECK=true

# vite-vue3:
# image: fullstack-blog-vite-vue3:dev
# ports:
# - "3000:3000"
# volumes:
# - ./app/vite-vue3/src:/usr/src/fullstack-blog/app/vite-vue3/src
# - ./app/vite-vue3/vite.config.docker.ts:/usr/src/fullstack-blog/app/vite-vue3/vite.config.ts
# - ./app/vite-vue3/index.html:/usr/src/fullstack-blog/app/vite-vue3/index.html
# - ./app/vite-vue3/public:/usr/src/fullstack-blog/app/vite-vue3/public
# - ./.prettierrc.json:/usr/src/fullstack-blog/.prettierrc.json
# - ./.prettierignore:/usr/src/fullstack-blog/.prettierignore
# environment:
# - NODE_ENV=development
vite-vue3:
image: fullstack-blog-vite-vue3:dev
ports:
- "3000:3000"
volumes:
- ./app/vite-vue3/src:/usr/src/fullstack-blog/app/vite-vue3/src
- ./app/vite-vue3/vite.config.docker.ts:/usr/src/fullstack-blog/app/vite-vue3/vite.config.ts
- ./app/vite-vue3/index.html:/usr/src/fullstack-blog/app/vite-vue3/index.html
- ./app/vite-vue3/public:/usr/src/fullstack-blog/app/vite-vue3/public
- ./.prettierrc.json:/usr/src/fullstack-blog/.prettierrc.json
- ./.prettierignore:/usr/src/fullstack-blog/.prettierignore
environment:
- NODE_ENV=development
- VITE_APP_SOCKET_SERVER=http://express-server:8002

express-server:
restart: always
Expand All @@ -60,6 +61,9 @@ services:
- PORT=8002
- MYSQL_ROOT_PASSWORD=${MYSQL_ROOT_PASSWORD}
- MYSQL_DATABASE=${MYSQL_DATABASE}
- WEB_SOCKET_WHITE_LIST=http://localhost:3000
working_dir: /usr/src/fullstack-blog/app/express-server
command: ["pnpm", "docker-dev"]

# nestjs-server:
# restart: always
Expand Down
25 changes: 25 additions & 0 deletions docker-ops.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,31 @@
| |-- init.sql
```

## Docker 开发环境

前端均支持 Docker 开发环境。

后端暂时只支持 express-server。nest-server 由于文件更新检测问题,暂时无法支持 Docker 开发环境,后续再投入时间研究补充。

构建镜像:

```shell
sh build-dev-images.sh
```

启动服务:

```shell
docker compose --env-file .env.docker.local -f compose-dev.yml up -d
```

其中的`.env.docker.local`文件内容参照下面内容:

```
MYSQL_ROOT_PASSWORD=xxx
MYSQL_DATABASE=blog_db
```

## Docker 生产环境

1. 构建镜像
Expand Down
2 changes: 1 addition & 1 deletion legacy-ops.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ pnpm install
由于不是使用 docker compose,需要修改`vite.config.ts`中的`server`配置:

```
target: "http://localhost:8002",
target: "http://127.0.0.1:8012",
```

启动项目:
Expand Down
Loading

0 comments on commit 373b310

Please sign in to comment.