diff options
author | brandon flowers <brandonflowers@gmail.com> | 2013-05-04 13:38:54 -0400 |
---|---|---|
committer | brandon flowers <brandonflowers@gmail.com> | 2013-05-04 13:38:54 -0400 |
commit | 5986b9a64d1ebc19c2347593edd2cd33d634bace (patch) | |
tree | c883a10790b2a7a1df583cb09e1873a684a3763c /examples/nodejs-mongodb-mongoose-restify/server-http.js | |
parent | 404bea77ec9d8d23a94b4ee9e0d643b4871773d6 (diff) | |
download | backbonetutorials-5986b9a64d1ebc19c2347593edd2cd33d634bace.zip backbonetutorials-5986b9a64d1ebc19c2347593edd2cd33d634bace.tar.gz backbonetutorials-5986b9a64d1ebc19c2347593edd2cd33d634bace.tar.bz2 |
updates to restify example to add tests and Yeoman support
Diffstat (limited to 'examples/nodejs-mongodb-mongoose-restify/server-http.js')
-rw-r--r-- | examples/nodejs-mongodb-mongoose-restify/server-http.js | 100 |
1 files changed, 100 insertions, 0 deletions
diff --git a/examples/nodejs-mongodb-mongoose-restify/server-http.js b/examples/nodejs-mongodb-mongoose-restify/server-http.js new file mode 100644 index 0000000..223c250 --- /dev/null +++ b/examples/nodejs-mongodb-mongoose-restify/server-http.js @@ -0,0 +1,100 @@ + +var connect = require('connect'); +var fs = require('fs'); +var httpServer = require('http'); +var path = require('path'); + +var config = require('./config'); + +// localhost + +var httpPort = process.env.PORT || 8000; + +/* + + see README.md for a more detailed write up + +*/ + +//////////////////////////////////////////////////////// HTTP - sends html/js/css to the browswer + +var sendHTML = function( filePath, contentType, response ){ + + console.log('sendHTML: ' + filePath) ; + + path.exists(filePath, function( exists ) { + + if (exists) { + fs.readFile(filePath, function(error, content) { + if (error) { + response.writeHead(500); + response.end(); + } + else { + response.writeHead(200, { 'Content-Type': contentType }); + response.end(content, 'utf-8'); + } + }); + } + else { + response.writeHead(404); + response.end(); + } + }); +} + +var getFilePath = function(url) { + + console.log("url: " + url); + + var filePath = './public' + url; + + if (url == '/' ) filePath = './public/index.html'; + + console.log("filePath: " + filePath); + + return filePath; +} + +var getContentType = function(filePath) { + + var extname = path.extname(filePath); + var contentType = 'text/html'; + + switch (extname) { + case '.js': + contentType = 'text/javascript'; + break; + case '.css': + contentType = 'text/css'; + break; + } + + return contentType; +} + +var onHtmlRequestHandler = function(request, response) { + + console.log('onHtmlRequestHandler... request.url: ' + request.url) ; + + /* + when this is live, nodjitsu only listens on 1 port(80) so the httpServer will hear it first but + we need to direct the request to the mongodbServer + */ + if ( process.env.PORT && url === '/messages') { + + // pass the request to mongodbServer + + return; + } + + var filePath = getFilePath(request.url); + var contentType = getContentType(filePath); + + console.log('onHtmlRequestHandler... getting: ' + filePath) ; + + sendHTML(filePath, contentType, response); + +} + +httpServer.createServer(onHtmlRequestHandler).listen(httpPort);
\ No newline at end of file |