Last active
August 29, 2015 14:03
-
-
Save sTiLL-iLL/adace414d5d74525486d to your computer and use it in GitHub Desktop.
HTTP STREAMING SERVER... KNDsrvr.js... javascript for Node.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// programatic entry point for the server // | |
var router = require('./router.js'), | |
requestHandlers = require('./requestHandlers.js'), | |
webServer = require('./server.js'), | |
handle = {}; | |
// Unary referencing... each function maps -to- a specific request/params/method etc... // | |
handle['/'] = handle['/home'] = handle['/Home'] = handle['/index.html'] = handle['/Index.html'] = requestHandlers.startRoot; | |
handle['/About'] = handle['/about'] = requestHandlers.startAbout; | |
handle['/Contact'] = handle['/contact'] = requestHandlers.startContact; | |
webServer.startWebServer(router, handle); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// a list of functions that map to request types defined in the index module | |
var fs = require('fs'), rStrm = null, | |
sr = function (request, response, args) { | |
rStrm = fs.createReadStream('./index.html'); | |
rStrm.on('open', function () { | |
rStrm.pipe(response); | |
}); | |
rStrm.on('end', function () { | |
response.end(); | |
}); | |
rStrm.on('error', function (err) { | |
response.end(err); | |
}); | |
}; | |
function startRoot(rq, rs, args) { | |
return sr(rq, rs, args); | |
}; | |
exports.startRoot = startRoot; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var rt = function (request, response, handle, namedPath) { | |
var paths = namedPath.split('/'), | |
len = paths.length, i = 0; | |
paths.unshift(namedPath); | |
for (var i = 0; i <= len; i++) { | |
if (typeof handle[paths[i]] === 'function') { | |
handle[paths[i]](request, response); | |
console.log("routing path: " + paths[i]); | |
return; | |
} | |
else { | |
console.log("No request handler found for: " + namedPath); | |
response.writeHead(404, { "Content-Type": "text/html" }); | |
response.end("404 Not found"); | |
return; | |
} | |
} | |
}; | |
function rtRqst(rq, rs, h, np) { | |
return rt(rq, rs, h, np); | |
}; | |
exports.routeRequest = rtRqst; | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// http stream server... | |
// it supports duplex streams, and concurrant processes based on CPU core count | |
var clusters = require('cluster'), port = 8000, | |
http = require('http'), routingLogic = null, | |
CPUs = require('os').cpus().length, | |
fs = require('fs'), url = require('url'), | |
sw = function (router, handle) { | |
if (clusters.isMaster) { | |
for (var i = 0; i < CPUs; i++) { | |
clusters.fork(); | |
} | |
clusters.on('fork', function (worker) { | |
console.log('worker process forked...' + worker.id); | |
}); | |
clusters.on('listening', function (worker, address) { | |
console.log("worker connection: " + address.address + ":" + address.port); | |
}); | |
clusters.on('exit', function (worker, code, signal) { | |
console.log('worker ' + worker.process.pid + ' died'); | |
}); | |
} | |
else { | |
console.log("starting webServer on port:" + port); | |
setTimeout(function () { | |
var server = http.createServer(onRequest); | |
server.listen(process.env.PORT || port); | |
}, 1000); | |
}; | |
routingLogic = router.routeRequest; | |
function onRequest(request, response) { | |
var namedPath = url.parse(request.url).pathname; | |
routingLogic(request, response, handle, namedPath); | |
return; | |
}; | |
process.on('unhandledException', function (err) { | |
console.log(err); | |
}); | |
}; | |
function startWeb(r, h) { | |
return sw(r, h); | |
}; | |
exports.startWebServer = startWeb; | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment