-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTest.js
57 lines (41 loc) · 1.44 KB
/
Test.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
54
55
56
57
const puppeteer = require('puppeteer')
const jsonServer = require('json-server')
const fs = require('fs')
// Define the path to the db.json file
const DB_PATH = './db.json'
// Check if the db.json file exists, and create it if it doesn't
if (!fs.existsSync(DB_PATH)) {
fs.writeFileSync(DB_PATH, '{}')
}
// Load the data from the db.json file
const data = JSON.parse(fs.readFileSync(DB_PATH))
// Create a new JSON Server instance and configure it
const server = jsonServer.create()
const router = jsonServer.router(data)
const middlewares = jsonServer.defaults()
// Use the middlewares and router with the server
server.use(middlewares)
server.use(router)
// Start the server on port 3000
server.listen(3000, () => {
console.log('JSON Server is running')
})
// Intercept the network requests
async function interceptRequests() {
const browser = await puppeteer.launch()
const page = await browser.newPage()
await page.setRequestInterception(true)
page.on('request', (request) => {
console.log(request.method() + ' ' + request.url())
// Automatically create a new endpoint for this request
const path = request.url().replace(/^https?:\/\/[^\/]+/, '').replace(/\?.*$/, '')
router.get(path, (req, res) => {
const data = { message: 'This is a mock response for ' + request.url() }
res.json(data)
})
request.continue()
})
await page.goto('http://localhost:3000')
await browser.close()
}
interceptRequests()