Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

Listen method and prefix propertie #82

Closed
wants to merge 2 commits into from

2 participants

@exos

Hi!

I added a listen method, this is for implement the static server on instances of http server with anothers uses.

For example:

var http = require('http');
var io = require('socket.io');
var static = require('static').Server;

var httpd = http.createServer();

var sio = io.listen(httpd);
var staticServer = new static('./public');

staticServer.listen(httpd);

// Listen on port 8080, using socket.io and node-static
httpd.listen(8080);

To, you can only define the listen port:

var static = require('static').Server;

server = new static('./public');

server.listen(8080);

To, I added a prefix property for restrict waths is served:

var static = require('static').Server;

server = new static('.', {
    prefix: '/static'
});

server.listen(8080);

The value of this propertie, can be a string or an regular expresion:

var static = require('static').Server;

server = new static('.', {
    prefix: /^\/(images|scripts|styles)\//
});

server.listen(8080);

or

var static = require('static').Server;

server = new static('.', {
    prefix: /\.(jpg|png|gif|ico|css|js|txt)$/i
});

server.listen(8080);

Sorry my english, I hope that changes served.

@phstc phstc closed this
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Dec 31, 2012
  1. @exos

    Adding listen method

    exos authored
  2. @exos

    adding option prefix

    exos authored
This page is out of date. Refresh to see the latest.
Showing with 59 additions and 0 deletions.
  1. +21 −0 examples/file-server-simple.js
  2. +38 −0 lib/node-static.js
View
21 examples/file-server-simple.js
@@ -0,0 +1,21 @@
+
+var http = require('http');
+var static = require('../lib/node-static');
+
+
+//
+// Create a http server
+//
+
+var httpd = http.createServer();
+
+//
+// Create a node-static server to serve the current directory
+//
+var file = new static.Server('.');
+
+file.listen(httpd);
+
+httpd.listen(8080);
+
+console.log("> node-static is listening on http://127.0.0.1:8080");
View
38 lib/node-static.js
@@ -21,6 +21,7 @@ exports.Server = function (root, options) {
this.root = path.resolve(root || '.');
this.options = options || {};
this.cache = 3600;
+ this.prefix = null;
this.defaultHeaders = {};
this.options.headers = this.options.headers || {};
@@ -39,6 +40,15 @@ exports.Server = function (root, options) {
this.serverInfo = 'node-static/' + exports.version.join('.');
}
+ if ('prefix' in this.options) {
+ this.prefix = this.options.prefix;
+
+ if (typeof this.prefix == "string") {
+ this.prefix = RegExp("^"+ this.prefix.replace(/([.?*+^$[\]\\(){}|-])/g, "\\$1"));
+ }
+
+ }
+
this.defaultHeaders['server'] = this.serverInfo;
if (this.cache !== false) {
@@ -173,6 +183,13 @@ exports.Server.prototype.serve = function (req, res, callback) {
var pathname = decodeURI(url.parse(req.url).pathname);
+ if (this.prefix) {
+ if (!pathname.match(this.prefix)) {
+ // Prefix don't match, ignore.
+ return;
+ }
+ }
+
var finish = function (status, headers) {
that.finish(status, headers, req, res, promise, callback);
};
@@ -270,3 +287,24 @@ exports.Server.prototype.stream = function (pathname, files, buffer, res, callba
})(files.slice(0), 0);
};
+exports.Server.prototype.listen = function () {
+
+ var httpd = {};
+ var self = this;
+
+ if (typeof arguments[0] == "object") {
+ httpd = arguments[0];
+ } else if (arguments[0] == "number") {
+ httpd = new http.createServer();
+ httpd.listen(arguments[0]);
+ } else {
+ throw new Error("Invalid arguments");
+ }
+
+ httpd.on('request', function (req, res) {
+ self.serve(req, res);
+ });
+
+ return httpd;
+
+};
Something went wrong with that request. Please try again.