-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
45 lines (35 loc) · 989 Bytes
/
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
'use strict'
const util = require('util')
const HttpAcceptSink = require('./helpers/http-accept-sink')
const BufferedSource = require('./helpers/buffered-source')
let Server
try {
Server = require('./build/Release/addon').Server
} catch (e) {
Server = require('./build/Debug/addon').Server
}
const ip = process.argv[2]
const port = Number(process.argv[3])
const server = new Server()
server.listen(ip, port, socket => {
try {
const httpAccept = new HttpAcceptSink()
const queue = new BufferedSource()
socket.sink.bindSource(queue)
httpAccept.bindSource(socket.source, error => {
if (error) {
console.error('ERROR!', error)
}
// Close socket if necessary
})
httpAccept.on('header', header => {
queue.send("HTTP/1.1 200 OK\nContent-Type: text/plain\nContent-Length: 13\n\nHello world!\n")
queue.end()
})
socket.start()
httpAccept.start()
} catch (err) {
console.error(err)
throw(err)
}
})