Permalink
Browse files

Adding files.

  • Loading branch information...
1 parent 696e751 commit 46c0b2cbee7b545ef977318da31fd60ea36fc2f1 Atsuya Takagi committed Aug 13, 2010
Showing with 126 additions and 0 deletions.
  1. +16 −0 example/server.js
  2. +10 −0 example/static/index.html
  3. +4 −0 index.js
  4. +96 −0 lib/staticresource/handler.js
View
@@ -0,0 +1,16 @@
+var http = require('http');
+var fs = require('fs');
+var url = require('url');
+var staticResource = require('../');
+
+var handler = staticResource.createHandler(fs.realpathSync('./static'));
+
+var server = http.createServer(function(request, response) {
+ var path = url.parse(request.url).pathname;
+ if(!handler.handle(path, request, response)) {
+ response.writeHead(404);
+ response.write('404');
+ response.end();
+ }
+});
+server.listen(8080);
@@ -0,0 +1,10 @@
+<!doctype html>
+<html lang="en">
+ <head>
+ <meta charset="utf-8">
+ <title>index.html</title>
+ </head>
+ <body>
+ <h1>Yay! This file is brought to you by static-resource!</h1>
+ </body>
+</html>
View
@@ -0,0 +1,4 @@
+exports.Handler = require('./lib/staticresource/handler');
+exports.createHandler = function(rootPath) {
+ return new exports.Handler(rootPath);
+};
@@ -0,0 +1,96 @@
+var sys = require('sys');
+var fs = require('fs');
+var path = require('path');
+
+Handler = module.exports = function(rootPath) {
+ this.rootPath = rootPath;
+ this.defaultContentType = 'application/octet-stream';
+ this.contentTypes = {
+ '.json': 'application/json',
+ '.js': 'application/javascript',
+ '.gif': 'image/gif',
+ '.jpg': 'image/jpeg',
+ '.jpeg': 'image/jpeg',
+ '.png': 'image/png',
+ '.svg': 'image/svg+xml',
+ '.css': 'text/css',
+ '.html': 'text/html',
+ '.txt': 'text/plain',
+ '.xml': 'text/xml'
+ };
+};
+
+Handler.prototype.handle = function(requestPath, request, response) {
+ var handled = false;
+ var resourcePath = this.rootPath+requestPath;
+
+ try {
+ var stat = fs.statSync(resourcePath);
+ if(stat.isFile()) {
+ var contentType = this._getContentType(resourcePath);
+ response.writeHead(200, {'Content-Type': contentType});
+ this._streamFile(resourcePath, request, response);
+ response.end();
+
+ handled = true;
+ }
+ } catch(error) {
+ //console.log(sys.inspect(error));
+ }
+
+ return handled;
+};
+
+Handler.prototype.addContentType = function(extension, contentType) {
+ var key = extension.toLowerCase();
+ if(!(key in this.contentTypes)) {
+ this.contentTypes[key] = contentType;
+ }
+}
+
+Handler.prototype.removeContentType = function(extension) {
+ var key = extension.toLowerCase();
+ if(key in this.contentTypes) {
+ delete this.contentTypes[key];
+ }
+}
+
+Handler.prototype._streamFile = function(resourcePath, request, response) {
+ // todo: do streaming instead of reading in entire file contents at once
+ response.write(fs.readFileSync(resourcePath));
+ /*
+ var buffer = new Buffer(1024);
+ //console.log(sys.inspect(buffer));
+ var bytesRead = 0;
+ var position = 0;
+
+ var fd = fs.openSync(resourcePath, 'r');
+ if(fd) {
+ while((bytesRead = fs.readSync(fd, buffer, 0, 1024, position)) > 0) {
+ console.log('bytesRead: '+bytesRead+', position: '+position);
+ response.write(buffer.toString('binary', 0, bytesRead));
+ //response.write(buffer.toString('binary', 0, bytesRead));
+ //response.write(buffer);
+ position += bytesRead;
+ }
+ fs.closeSync(fd);
+ }
+ */
+};
+
+Handler.prototype._getContentType = function(resourcePath) {
+ var contentType = null;
+
+ var extension = path.extname(resourcePath).toLowerCase();
+ for(var key in this.contentTypes) {
+ if(extension == key) {
+ contentType = this.contentTypes[key];
+ break;
+ }
+ }
+ if(contentType == null) {
+ contentType = this.defaultContentType;
+ }
+
+ return contentType;
+};

0 comments on commit 46c0b2c

Please sign in to comment.