/
index.js
executable file
·80 lines (65 loc) · 2.05 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
79
80
#!/usr/bin/env node
'use strict'
var getPort = require('get-port')
var server = require('net').createServer()
var cid = 0
module.exports = server // for testing
onEmit(server, { ignore: ['connection', 'listening', 'error'] }, function (eventName) {
console.log('[server] event:', eventName)
})
server.on('connection', function (c) {
var gotData = false
var _cid = ++cid
console.log('[server] event: connection (socket#%d)', _cid)
onEmit(c, { ignore: ['lookup', 'error'] }, function (eventName) {
console.log('[socket#%d] event:', _cid, eventName)
})
c.on('lookup', function (err, address, family) {
if (err) {
console.log('[socket#%d] event: lookup (error: %s)', _cid, err.message)
} else {
console.log('[socket#%d] event: lookup (address: %s, family: %s)', _cid, address, family)
}
})
c.on('data', function (chunk) {
console.log('--> ' + chunk.toString().split('\n').join('\n--> '))
if (!gotData) {
gotData = true
c.write('HTTP/1.1 200 OK\r\n')
c.write('Date: ' + (new Date()).toString() + '\r\n')
c.write('Connection: close\r\n')
c.write('Content-Type: text/plain\r\n')
c.write('Access-Control-Allow-Origin: *\r\n')
c.write('\r\n')
setTimeout(function () {
c.end()
}, 2000)
}
c.write(chunk.toString())
})
c.on('error', function (err) {
console.log('[socket#%d] event: error (msg: %s)', _cid, err.message)
})
})
server.on('listening', function () {
var port = server.address().port
console.log('[server] event: listening (port: %d)', port)
})
server.on('error', function (err) {
console.log('[server] event: error (msg: %s)', err.message)
})
var port = process.argv[2] || process.env.PORT
if (port) {
server.listen(port)
} else {
getPort({ port: 3000 }).then(function (port) {
server.listen(port)
})
}
function onEmit (emitter, opts, cb) {
var emitFn = emitter.emit
emitter.emit = function (eventName) {
if (opts.ignore.indexOf(eventName) === -1) cb.apply(null, arguments)
return emitFn.apply(emitter, arguments)
}
}