Last active
March 28, 2017 01:23
-
-
Save JBreit/572dfdb4a7dec695376e35bc54dfc5fd to your computer and use it in GitHub Desktop.
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 http = require('http'), | |
ecstatic = require("ecstatic"), | |
fileServer = ecstatic({root: "./public"}), | |
config = require('./options'), | |
router = require('./routes'); | |
var normalizePort = function (val) { | |
'use strict'; | |
var _port = parseInt(val, 10); | |
if (isNaN(_port)) { | |
return val; | |
} | |
if (_port >= 0) { | |
return _port; | |
} | |
return false; | |
}; | |
var host = process.env.HOST || config.host; | |
var port = normalizePort(process.env.PORT || config.port); | |
var handleRequest = function (request, response) { | |
'use strict'; | |
request.db = {}; | |
if (!router.resolve(request, response)) { | |
fileServer(request, response); | |
} | |
}; | |
http.createServer(handleRequest).listen(port, host, function () { | |
'use strict'; | |
process.stdout.write('> Server listening at ' + host + ':' + port + '\n'); | |
}); | |
// Client !!!!!! | |
var options = { | |
hostname: '127.0.0.1', | |
port: '8080', | |
path: '/' | |
}; | |
function handleResponse(response) { | |
'use strict'; | |
var data = ''; | |
response | |
.on('data', function (chunk) { | |
data += chunk; | |
}) | |
.on('end', function () { | |
console.log("Response Status:", response.statusCode); | |
console.log("Response Headers:", response.headers); | |
console.log(data); | |
}); | |
} | |
http.request(options, function (response) { | |
'use strict'; | |
handleResponse(response); | |
}).end(); |
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
{ | |
"host": "127.0.0.1", | |
"port": 8080, | |
"ssl": { | |
"active": false, | |
"key": "ssl/server.key", | |
"cert": "ssl/server.crt", | |
"ca": "ssl/client.crt" | |
} | |
} |
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 Router = function () { | |
'use strict'; | |
this.routes = []; | |
}; | |
Router.prototype.register = function (method, url, handler) { | |
'use strict'; | |
this.routes.push({method: method, url: url, handler: handler}); | |
}; | |
Router.prototype.resolve = function (request, response) { | |
'use strict'; | |
var path = require('url').parse(request.url).pathname; | |
return this.routes.some(function (route) { | |
var match = route.url.exec(path); | |
if (!match || route.method !== request.method) { | |
return false; | |
} | |
var urlParts = match.slice(1).map(decodeURIComponent); | |
route.handler.apply(null, [request, response].concat(urlParts)); | |
return true; | |
}); | |
}; | |
module.exports = Router; |
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 fs = require('fs'), | |
Router = require('./router'), | |
router = new Router(); | |
var respond = function (response, status, data, type) { | |
'use strict'; | |
response.writeHead(status, {"Content-Type": type || "text/plain"}); | |
if (data && data.pipe) { | |
data.pipe(response); | |
} else { | |
response.end(data); | |
} | |
}; | |
var respondAsJSON = function (response, status, data) { | |
'use strict'; | |
respond(response, status, JSON.stringify(data), "application/json"); | |
}; | |
router.register('GET', /^\/$/, function (request, response) { | |
'use strict'; | |
console.log('db: ', request.db); | |
var rstream = fs.createReadStream('./views/index.html', {encoding: 'utf8'}), | |
data = ''; | |
rstream | |
.on('data', function (chunk) { | |
data += chunk; | |
}) | |
.on('end', function () { | |
//console.log('> Data: ', data); | |
}); | |
respond(response, 200, rstream.pipe(response), 'text/html'); | |
}); | |
router.register('GET', /^\/api\/$/, function (request, response) { | |
'use strict'; | |
//console.log(typeof request); | |
respondAsJSON(response, 200, {test: 'shit', more: 'fuckery', shit: 'goes splat!'}); | |
}); | |
router.register('GET', /^\/list$/, function (request, response) { | |
'use strict'; | |
//console.log(typeof request); | |
var rstream = fs.createReadStream('./views/list.html', {encoding: 'utf8'}), | |
data = ''; | |
rstream | |
.on('data', function (chunk) { | |
data += chunk; | |
}) | |
.on('end', function () { | |
//console.log('> Data: ', data); | |
}); | |
respond(response, 200, rstream.pipe(response), 'text/html'); | |
}); | |
router.register('GET', /^\/test\/$/, function (request, response) { | |
'use strict'; | |
//console.log(typeof request); | |
respond(response, 200, '<h1>Test</h1>', 'text/html'); | |
}); | |
module.exports = router; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment