Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
T
taobao-mini-template
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
2
Issues
2
List
Board
Labels
Milestones
Merge Requests
1
Merge Requests
1
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
qinhaitao
taobao-mini-template
Commits
54c25393
Commit
54c25393
authored
Mar 22, 2021
by
王波
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
增加mockserver支持
parent
db3f7638
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
109 additions
and
1 deletion
+109
-1
package.json
server/duibats/mockServer/package.json
+1
-1
server_ts_debug.js
server/duibats/mockServer/server_ts_debug.js
+107
-0
package.json
server/duibats/package.json
+1
-0
No files found.
server/duibats/mockServer/package.json
View file @
54c25393
...
...
@@ -5,7 +5,7 @@
"main"
:
"server.js"
,
"scripts"
:
{
"test"
:
"echo
\"
Error: no test specified
\"
&& exit 1"
,
"start"
:
"nodemon --watch sdk --watch ../ --watch server
.js server
.js mock"
"start"
:
"nodemon --watch sdk --watch ../ --watch server
_ts_debug.js server_ts_debug
.js mock"
},
"author"
:
""
,
"license"
:
"ISC"
,
...
...
server/duibats/mockServer/server_ts_debug.js
0 → 100644
View file @
54c25393
/** @format */
var
express
=
require
(
'express'
)
var
bodyParser
=
require
(
'body-parser'
)
var
cookieParser
=
require
(
'cookie-parser'
)
var
{
createProxyMiddleware
}
=
require
(
'http-proxy-middleware'
)
const
routers
=
require
(
'../src/index'
).
default
const
dbs
=
require
(
'../src/db'
)
const
{
port
,
serverProxy
}
=
require
(
'./package.json'
)
var
app
=
express
()
app
.
use
(
cookieParser
())
app
.
use
(
bodyParser
.
urlencoded
({
extended
:
true
})
)
app
.
use
(
bodyParser
.
json
())
app
.
get
(
'/'
,
(
req
,
res
)
=>
{
res
.
json
({
message
:
`ok`
})
})
// 执行本地云函数方法
app
.
post
(
'/handler'
,
async
(
req
,
res
)
=>
{
console
.
log
(
req
.
body
)
let
{
data
,
openId
,
db
}
=
req
.
body
if
(
!
db
)
{
res
.
json
({
message
:
`参数错误,缺少数据库db`
})
}
if
(
!
openId
)
{
res
.
json
({
message
:
`参数错误,缺少openId`
})
}
try
{
let
query
=
JSON
.
parse
(
JSON
.
stringify
(
data
))
}
catch
(
e
)
{
res
.
json
({
message
:
`查询参数格式错误`
})
}
let
{
handler
,
data
:
queryData
}
=
data
if
(
!
handler
)
{
res
.
json
({
message
:
`格式错误, handler参数不能为空`
})
}
if
(
!
queryData
)
{
res
.
json
({
message
:
`格式错误, data参数不能为空`
})
}
if
(
!
process
.
db
)
{
process
.
db
=
db
}
req
.
context
=
{
openId
,
db
,
env
:
'mock'
,
data
:
queryData
,
handler
}
if
(
routers
[
handler
])
{
let
result
=
await
routers
[
handler
](
req
.
context
)
res
.
json
(
result
)
}
else
{
res
.
json
({
message
:
`函数
${
handler
}
不存在`
})
}
})
// 获取本地export的数据库表及云函数
app
.
get
(
'/getDBHandlers'
,
async
(
req
,
res
)
=>
{
let
handlers
=
routers
// dbsInfo
let
dbArray
=
[]
let
handlerArray
=
[]
Object
.
keys
(
dbs
).
map
(
v
=>
{
dbArray
.
push
(
dbs
[
v
])
})
Object
.
keys
(
handlers
).
map
(
v
=>
{
handlerArray
.
push
(
v
)
})
console
.
log
(
dbArray
,
handlerArray
)
res
.
json
({
dbs
:
dbArray
,
handlers
:
handlerArray
,
success
:
true
})
})
const
ProxyOption
=
{
target
:
serverProxy
.
target
,
changeOrigin
:
true
,
pathRewrite
:
(
path
,
req
)
=>
{
console
.
log
(
`path`
,
path
)
return
path
.
replace
(
'/proxy'
,
''
)
},
onProxyReq
:
(
proxyReq
,
req
,
res
)
=>
{
console
.
log
(
`req.body`
,
req
.
body
)
if
(
req
.
body
)
{
let
bodyData
=
JSON
.
stringify
(
req
.
body
)
console
.
log
(
`bodyData`
,
bodyData
)
// incase if content-type is application/x-www-form-urlencoded -> we need to change to application/json
proxyReq
.
setHeader
(
'Content-Type'
,
'application/json'
)
proxyReq
.
setHeader
(
'Content-Length'
,
Buffer
.
byteLength
(
bodyData
))
// stream the content
proxyReq
.
write
(
bodyData
)
}
}
}
app
.
use
(
'/proxy'
,
createProxyMiddleware
(
ProxyOption
))
app
.
listen
(
port
||
5555
)
server/duibats/package.json
View file @
54c25393
...
...
@@ -31,6 +31,7 @@
"eslint-config-prettier"
:
"^6.11.0"
,
"eslint-plugin-prettier"
:
"^3.1.4"
,
"prettier"
:
"^2.0.5"
,
"ts-node"
:
"^9.0.0"
,
"typescript"
:
"^3.9.7"
}
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment