Skip to content

Commit 1a6a25f

Browse files
committed
支持容器
1 parent 2ce758c commit 1a6a25f

File tree

4 files changed

+32
-107
lines changed

4 files changed

+32
-107
lines changed

.dockerignore

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
node_modules
2+
npm-debug.log
3+
Dockerfile
4+
.dockerignore
5+
.git
6+
.gitignore

Dockerfile

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
FROM node:18-alpine
2+
3+
WORKDIR /app
4+
5+
COPY package*.json ./
6+
7+
RUN npm install
8+
9+
COPY . .
10+
11+
EXPOSE 3000
12+
13+
CMD ["npm", "start"]

README.md

Lines changed: 2 additions & 107 deletions
Original file line numberDiff line numberDiff line change
@@ -12,119 +12,14 @@
1212
- 🌐 支持 RESTful API
1313
- 📊 内置常用数据模板
1414

15-
## 安装
16-
17-
```bash
18-
npm install mockaroo
19-
```
20-
21-
或者使用 yarn:
22-
23-
```bash
24-
yarn add mockaroo
25-
```
26-
2715
## 快速开始
2816

29-
1. 创建配置文件 `mock.config.js`
30-
31-
```javascript
32-
module.exports = {
33-
port: 3000,
34-
routes: {
35-
'/api/users': {
36-
method: 'GET',
37-
response: {
38-
code: 200,
39-
data: [
40-
{ id: 1, name: 'John Doe' },
41-
{ id: 2, name: 'Jane Smith' }
42-
]
43-
}
44-
}
45-
}
46-
};
47-
```
48-
49-
2. 启动服务:
17+
1. 启动服务:
5018

5119
```bash
52-
npx mockaroo start
20+
pnpm start
5321
```
5422

55-
## 高级配置
56-
57-
### 动态数据
58-
59-
支持使用模板函数生成动态数据:
60-
61-
```javascript
62-
{
63-
'/api/random': {
64-
method: 'GET',
65-
response: () => ({
66-
id: Math.random(),
67-
timestamp: Date.now()
68-
})
69-
}
70-
}
71-
```
72-
73-
### 延迟响应
74-
75-
模拟网络延迟:
76-
77-
```javascript
78-
{
79-
'/api/slow': {
80-
method: 'GET',
81-
delay: 2000, // 延迟 2 秒
82-
response: { message: 'Slow response' }
83-
}
84-
}
85-
```
86-
87-
### 条件响应
88-
89-
根据请求参数返回不同响应:
90-
91-
```javascript
92-
{
93-
'/api/conditional': {
94-
method: 'POST',
95-
handler: (req) => {
96-
if (req.body.type === 'error') {
97-
return {
98-
status: 500,
99-
response: { error: 'Server Error' }
100-
};
101-
}
102-
return {
103-
status: 200,
104-
response: { message: 'Success' }
105-
};
106-
}
107-
}
108-
}
109-
```
110-
111-
## API 文档
112-
113-
### CLI 命令
114-
115-
- `mockaroo start`: 启动 mock 服务器
116-
- `mockaroo init`: 创建配置文件模板
117-
- `mockaroo --help`: 显示帮助信息
118-
119-
### 配置选项
120-
121-
| 选项 | 类型 | 默认值 | 说明 |
122-
|------|------|--------|------|
123-
| port | number | 3000 | 服务器端口 |
124-
| host | string | 'localhost' | 服务器主机 |
125-
| cors | boolean | true | 是否允许跨域 |
126-
| delay | number | 0 | 全局延迟(毫秒) |
127-
12823
## 贡献指南
12924

13025
欢迎提交 Issue 和 Pull Request!在提交 PR 之前,请确保:

docker-compose.yml

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
version: '3'
2+
services:
3+
mock-server:
4+
build: .
5+
ports:
6+
- "3000:3000"
7+
volumes:
8+
- .:/app
9+
- /app/node_modules
10+
environment:
11+
- NODE_ENV=development

0 commit comments

Comments
 (0)