-
Notifications
You must be signed in to change notification settings - Fork 1
/
server.js
54 lines (43 loc) · 1.32 KB
/
server.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
const http = require('http')
const fs = require('fs')
const path = require('path')
const { URL } = require('url')
const tokenEncoding = 'base64'
const data = Array(100)
.fill(null)
.map((_, i) => ({ id: i + 1, value: 'some value ' + (i + 1) }))
function api(req, res) {
const search = new URL(req.url, `http://${req.headers.host}`).searchParams
const pageSize = +search.get('pageSize')
const pageToken = search.get('pageToken')
const pageNumber = pageToken
? +Buffer.from(pageToken, tokenEncoding).toString().split(':')[1]
: 1
const start = pageSize * (pageNumber - 1)
const end = start + pageSize
const result = {
data: data.slice(start, end),
nextPage:
end < data.length
? Buffer.from(`pageNumber:${pageNumber + 1}`).toString(tokenEncoding)
: null,
}
setTimeout(() => {
res.end(JSON.stringify(result))
}, 1000)
}
function static(req, res) {
const filePath = path.join(__dirname, req.url)
if (fs.existsSync(filePath) && fs.statSync(filePath).isFile()) {
return res.end(fs.readFileSync(filePath))
}
return res.end(fs.readFileSync(path.join(__dirname, 'index.html')))
}
http
.createServer((req, res) => {
if (/^\/api/.test(req.url)) {
return api(req, res)
}
static(req, res)
})
.listen(4000, () => console.log(`listening on port 4000`))