-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
78 lines (65 loc) · 2 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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
import { createServer } from 'node:http'
import Hold from 'hold-this'
/**
* Creates a HTTP server decorated with an instance of the data store
*
* @param {Object} options - server options
* @param {number} options.port - port number to listen on
* @returns {http.Server}
* @example
* const server = Server()
*/
export function Server ({ port = 3000 } = {}) {
const server = createServer()
server.holder = Hold()
server.on('request', (req, res) => {
if (req.method !== 'POST') {
res.writeHead(405, { 'Content-Type': 'application/json' })
res.end('Method Not Allowed\n')
return false
}
if (req.headers['content-type'] !== 'application/json') {
res.writeHead(415, { 'Content-Type': 'application/json' })
res.end('Unsupported Media Type\n')
return false
}
let body = ''
req.on('data', (chunk) => { body += chunk })
req.on('end', () => {
const parsedBody = JSON.parse(body)
const { cmd, topic, key, value, options } = parsedBody
const data = server.holder[cmd](topic, key, value, options)
res.writeHead(200, { 'Content-Type': 'application/json' })
res.end(JSON.stringify(data))
})
})
server.listen(port)
return server
}
// When this file is run directly from node (main module)
// setup a server, and run a test demonstrating using fetch to set and
// get data from the server
if (process.argv[1] === import.meta.filename) {
Server()
// Write the record that we'll be fetching
await fetch('http://localhost:3000', {
headers: {
'Content-Type': 'application/json'
},
method: 'POST',
body: JSON.stringify({
cmd: 'set', topic: 'http', key: 'test', value: 'test1'
})
})
// Fetch the record previously set
const response = await fetch('http://localhost:3000', {
headers: {
'Content-Type': 'application/json'
},
method: 'POST',
body: JSON.stringify({
cmd: 'get', topic: 'http', key: 'test'
})
}).then(response => response.json())
console.log(response)
}