-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
53 lines (49 loc) · 1.65 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
const express = require('express')
const bodyParser = require('body-parser')
const fs = require('fs')
const doc = require('./render/doc')
const helper = require('./helper')
const start = (rootDirPath, port=3000) => {
const app = express()
// set bodyparser
app.use(bodyParser.urlencoded({ extended: true }))
app.use(bodyParser.json())
// add route config
const routes = []
fs.readdirSync(rootDirPath).forEach((dir) => {
fs.readdirSync(`${rootDirPath}/${dir}`).forEach((file) => {
const { mockPath, mockRequest, mockResponse } = require(`${rootDirPath}/${dir}/${file}`)
const urlPath = `/${dir}${mockPath.path}`
switch(mockPath.method) {
case "GET":
app.get(urlPath, (req, res) => helper.apiCallback(req, res, mockRequest, mockResponse))
break
case "POST":
app.post(urlPath, (req, res) => helper.apiCallback(req, res, mockRequest, mockResponse))
break
case "PUT":
app.put(urlPath, (req, res) => helper.apiCallback(req, res, mockRequest, mockResponse))
break
case "DELETE":
app.delete(urlPath, (req, res) => helper.apiCallback(req, res, mockRequest, mockResponse))
break
default:
break
}
routes.push({
mockPath: {
path: urlPath,
method: mockPath.method
},
mockRequest,
mockResponse
})
})
})
app.get('/doc', (req, res) => res.send(doc.generateDoc(routes)))
app.use((req, res) => helper.generateErrRes(res, 404, "Page not found"))
app.listen(port, () => console.log(`Mock Server start on port ${port}!`))
}
module.exports = {
start
}