@@ -4,69 +4,51 @@ import auth from './auth';
4
4
import config from './config' ;
5
5
import rules from './rules' ;
6
6
import * as https from 'https' ;
7
- import { Http , noop , getInfo , formatResult , mockResult , authHandler , errorHandler , relayHandler , methodHandler } from './utils'
7
+ import { Http , noop , once , getInfo , formatResult , mockResult , authHandler , errorHandler , relayHandler , methodHandler } from './utils'
8
8
import * as fs from 'fs' ;
9
9
import { mock } from '../' ;
10
10
const server = jsonServer . create ( )
11
- // 路径从根目录开始?
12
- const router = jsonServer . router ( path . resolve ( process . cwd ( ) , 'db.json' ) )
13
11
14
- const createServer = ( option : mock . anyObject , callback ?: Function ) => {
15
- let config = option . https
16
- config = / ^ ( b o o l e a n | n u m b e r ) $ / . test ( typeof config ) ? config && { } : config
17
- let currentServer
18
- if ( config instanceof Object ) {
19
- if ( typeof config . static === 'function' ) {
20
- config . static ( jsonServer , server )
21
- } else {
22
- const middlewares = jsonServer . defaults ( {
23
- static : typeof config . static === 'string' ? config . static : path . resolve ( process . cwd ( ) , './public' )
24
- } )
25
- server . use ( middlewares )
26
- }
27
- if ( typeof config . key !== 'string' || typeof config . cert !== 'string' || config . key . length + config . cert . length === 0 ) {
28
- config . key = fs . readFileSync ( path . join ( __dirname , 'ssl/key.pem' ) )
29
- config . cert = fs . readFileSync ( path . join ( __dirname , 'ssl/cert.pem' ) )
30
- console . log ( "正在使用默认的证书配置" )
31
- }
32
- currentServer = https . createServer ( config , server ) . listen ( option . port , function ( ) {
33
- console . log ( )
34
- console . log ( `已启动json-server服务器 https://localhost:${ option . port } ` )
35
- console . log ( )
36
- typeof callback == 'function' && callback ( ) ;
37
- } )
12
+ // 静态文件服务
13
+ const useStaticServer = ( option ) => {
14
+ if ( typeof option . static === 'function' ) {
15
+ option . static ( jsonServer , server )
38
16
} else {
39
- currentServer = server . listen ( option . port , ( ) => {
40
- console . log ( )
41
- console . log ( `已启动json-server服务器 http://localhost:${ option . port } ` )
42
- console . log ( )
43
- typeof callback == 'function' && callback ( ) ;
17
+ const middlewares = jsonServer . defaults ( {
18
+ static : typeof option . static === 'string' ? option . static : path . resolve ( __dirname , './public' )
44
19
} )
20
+ server . use ( middlewares )
45
21
}
46
- currentServer . on ( 'error' , ( ...rest ) => {
47
- option . port ++
48
- createServer ( option , callback )
22
+ }
23
+
24
+ // 上传文件服务
25
+ const useUploadServer = ( option ) => {
26
+ const multer = require ( 'multer' )
27
+ const storage = multer . diskStorage ( {
28
+ destination : function ( req , file , cb ) {
29
+ const done = once ( cb )
30
+ if ( noop ( option . beforeUpload ) . call ( option , req , file , done ) !== false ) {
31
+ done ( null , option . uploadPath || path . join ( __dirname , '/uploads' ) )
32
+ }
33
+ } ,
34
+ filename : function ( req , file , cb ) {
35
+ const done = once ( cb )
36
+ if ( noop ( option . beforeWrite ) . call ( option , req , file , done ) !== false ) {
37
+ done ( null , file . originalname )
38
+ }
39
+ }
49
40
} )
41
+ const upload = multer ( { storage } )
42
+ server . use ( upload . any ( ) )
50
43
}
51
44
52
- /**
53
- * 启动mock服务
54
- * @func
55
- * @param {object } option mock 服务配置
56
- * @param {mockData } option.mockData - mock数据json(支持 mockjs 中的写法)
57
- * @param {headoption= } option.headoption - 服务端请求头信息配置
58
- * @param {boolean= } option.crossDomain - 是否跨域 (便于在不设置请求头时, 快速配置跨域)
59
- * @param {number= } port - 服务器端口
60
- */
61
- const Server = ( option : mock . anyObject , callback ?: Function ) => {
62
- option = Object . assign ( {
63
- port : 3030 ,
64
- crossDomain : true ,
65
- headoption : null ,
66
- mockData : { } ,
67
- bounded : ! ! option . loginUrl
68
- } , option )
45
+ const useRouter = ( option ) => {
46
+
47
+ // 路径从根目录开始?
48
+ const router = jsonServer . router ( path . resolve ( process . cwd ( ) , 'db.json' ) )
49
+
69
50
let mockData = option . mockData
51
+
70
52
// To handle POST, PUT and PATCH you need to use a body-parser
71
53
// You can use the one used by JSON Server
72
54
server . use ( jsonServer . bodyParser )
@@ -86,8 +68,9 @@ const Server = (option: mock.anyObject, callback?: Function) => {
86
68
method,
87
69
urlKey,
88
70
params,
89
- headConfig
90
- } = getInfo ( req , option , config . crossDomain )
71
+ headConfig,
72
+ extra
73
+ } = getInfo ( req , option , config . crossDomain , res )
91
74
let result = { }
92
75
// 是否需要将接口的处理逻辑交由json-server
93
76
let transfer = method === 'post' && router . db . __wrapped__ . hasOwnProperty ( urlKey )
@@ -99,7 +82,8 @@ const Server = (option: mock.anyObject, callback?: Function) => {
99
82
data,
100
83
method,
101
84
params,
102
- result
85
+ result,
86
+ extra
103
87
} )
104
88
const allOption = {
105
89
req,
@@ -112,7 +96,8 @@ const Server = (option: mock.anyObject, callback?: Function) => {
112
96
option,
113
97
headConfig,
114
98
result,
115
- transfer
99
+ transfer,
100
+ extra
116
101
}
117
102
// 2. 处理鉴权
118
103
if ( authHandler ( allOption ) === false ) { return }
@@ -146,7 +131,8 @@ const Server = (option: mock.anyObject, callback?: Function) => {
146
131
method,
147
132
urlKey,
148
133
params,
149
- } = getInfo ( req , option , config . crossDomain )
134
+ extra
135
+ } = getInfo ( req , option , config . crossDomain , res )
150
136
mockData = mockData || { }
151
137
let body = {
152
138
code : 201 ,
@@ -165,16 +151,71 @@ const Server = (option: mock.anyObject, callback?: Function) => {
165
151
method ,
166
152
params ,
167
153
JSON . parse ( JSON . stringify ( current [ method ] instanceof Object ? current [ method ] : { } ) ) ,
168
- {
154
+ Object . assign ( { } , extra , {
169
155
url,
170
156
body : JSON . parse ( JSON . stringify ( body ) )
171
- }
157
+ } )
172
158
) || body )
173
159
}
174
160
// post成功后, 对其返回数据进行包装
175
161
res . status ( 200 ) . jsonp ( body )
176
162
}
163
+
177
164
server . use ( router )
178
- createServer ( option , callback )
165
+ }
166
+
167
+ const createServer = ( option : mock . anyObject , callback ?: Function ) => {
168
+ let config = option . https
169
+ config = / ^ ( b o o l e a n | n u m b e r ) $ / . test ( typeof config ) ? config && { } : config
170
+ let currentServer
171
+ useStaticServer ( option )
172
+ useUploadServer ( option )
173
+ useRouter ( option )
174
+
175
+ if ( config instanceof Object ) {
176
+ if ( typeof config . key !== 'string' || typeof config . cert !== 'string' || config . key . length + config . cert . length === 0 ) {
177
+ config . key = fs . readFileSync ( path . join ( __dirname , 'ssl/key.pem' ) )
178
+ config . cert = fs . readFileSync ( path . join ( __dirname , 'ssl/cert.pem' ) )
179
+ console . log ( "正在使用默认的证书配置" )
180
+ }
181
+ currentServer = https . createServer ( config , server ) . listen ( option . port , function ( ) {
182
+ option . serverUrl = `https://localhost:${ option . port } `
183
+ console . log ( )
184
+ console . log ( `已启动json-server服务器 ${ option . serverUrl } ` )
185
+ console . log ( )
186
+ typeof callback == 'function' && callback ( ) ;
187
+ } )
188
+ } else {
189
+ currentServer = server . listen ( option . port , ( ) => {
190
+ option . serverUrl = `http://localhost:${ option . port } `
191
+ console . log ( )
192
+ console . log ( `已启动json-server服务器 ${ option . serverUrl } ` )
193
+ console . log ( )
194
+ typeof callback == 'function' && callback ( ) ;
195
+ } )
196
+ }
197
+ currentServer . on ( 'error' , ( ...rest ) => {
198
+ option . port ++
199
+ createServer ( option , callback )
200
+ } )
201
+ }
202
+
203
+ /**
204
+ * 启动mock服务
205
+ * @func
206
+ * @param {object } option mock 服务配置
207
+ * @param {mockData } option.mockData - mock数据json(支持 mockjs 中的写法)
208
+ * @param {headoption= } option.headoption - 服务端请求头信息配置
209
+ * @param {boolean= } option.crossDomain - 是否跨域 (便于在不设置请求头时, 快速配置跨域)
210
+ * @param {number= } port - 服务器端口
211
+ */
212
+ const Server = ( option : mock . anyObject , callback ?: Function ) => {
213
+ createServer ( Object . assign ( {
214
+ port : 3030 ,
215
+ crossDomain : true ,
216
+ headoption : null ,
217
+ mockData : { } ,
218
+ bounded : ! ! option . loginUrl
219
+ } , option ) , callback )
179
220
}
180
221
export default Server
0 commit comments