-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
50 lines (48 loc) · 1.23 KB
/
Copy pathserver.js
File metadata and controls
50 lines (48 loc) · 1.23 KB
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
const assert = require('assert').strict
const fs = require('fs')
const http = require('http')
const sb = require('../dist')
const type = new sb.ArrayType(
new sb.StructType({
name: new sb.StringType,
id: new sb.UnsignedShortType
})
)
const downloadType = new sb.ArrayType(
new sb.StructType({
name: new sb.StringType,
id: new sb.PointerType(new sb.UnsignedShortType)
})
)
const VALUE = [{name: 'John', id: 2}, {name: 'Jane', id: 10}]
const PORT = 8080
http.createServer((req, res) => {
if (req.url === '/uploadtest') {
sb.readValue({inStream: req, type}, (err, value) => {
if (err) {
res.end('Error occurred')
console.log(err)
}
else {
assert.deepEqual(value, VALUE)
console.log('Got upload')
res.end('Success')
}
})
}
else if (req.url.startsWith('/downloadtest')) {
sb.httpRespond({req, res, type: downloadType, value: VALUE})
console.log('Sent download')
}
else {
if (req.url === '/') req.url = '/client-test/files/index.html'
const readStream = fs.createReadStream(__dirname + '/..' + req.url)
readStream.on('error', err => {
console.log(err)
res.writeHead(404)
res.end('Error occurred')
})
readStream.pipe(res)
}
}).listen(PORT)
console.log('Listening on port ' + String(PORT))