Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

minor fixes. new examples.

  • Loading branch information...
commit 2c9de0f1cc7fd1af1a3fef091f3fd05aa2daf47f 1 parent 74e67be
@cainus authored
View
9 nextsimplest.js
@@ -0,0 +1,9 @@
+var Percolator = require('./percolator');
+
+var server = new Percolator();
+server.routeDirectory(__dirname + '/resources', function(err){
+ if (!!err) {console.log(err);}
+ server.listen(function(err){
+ console.log('server is listening on port ', server.port);
+ });
+});
View
35 percolator.js
@@ -8,23 +8,19 @@ var JsonResponder = require('./StatusManager').JsonResponder;
var _ = require('underscore');
Percolator = function(options){
+ options = options || {};
this.statusman = new StatusManager();
- this.options = options;
+ this.options = _.extend({port : 3000,
+ protocol : 'http',
+ resourcePath : '/'
+ }, options);
this.mediaTypes = new Reaper();
- this.port = options.port || 80;
- this.protocol = options.protocol || 'http';
- var protocol = this.protocol;
- this.resourceDir = options.resourceDir || './resources';
- this.resourcePath = options.resourcePath || '/api';
- this.staticDir = options.staticDir || './static';
- this.options = _.extend(options,
- {port : this.port,
- protocol : this.protocol,
- resourcePath : this.resourcePath,
- staticDir : this.staticDir,
- resourceDir : this.resourceDir});
+ this.port = this.options.port;
+ this.protocol = this.options.protocol;
+ this.resourcePath = this.options.resourcePath;
this.router = new Router(this.resourcePath);
var router = this.router;
+ var protocol = this.protocol;
this.router.onRequest = function(handler, req, res, cb){
handler.uri = new UriUtil(router, req.url, protocol, req.headers.host);
cb(null, handler);
@@ -32,7 +28,12 @@ Percolator = function(options){
this.assignErrorHandlers();
this.registerMediaTypes();
var that = this;
- this.server = express.createServer();
+ this.expressServer = express.createServer();
+ if (!!options.staticDir){
+ this.staticDir = options.staticDir;
+ this.expressServer.use(express['static'](this.staticDir));
+ }
+ this.expressServer.use(express.bodyParser()); // TODO does this work for PUT?!?!
this.router.on("route", function(resource){
that.decorateResource(resource);
});
@@ -169,12 +170,12 @@ Percolator.prototype.registerMediaTypes = function(){
};
Percolator.prototype.use = function(middleware){
- this.server.use(middleware);
+ this.expressServer.use(middleware);
};
-Percolator.prototype.listen = function(port, cb){
+Percolator.prototype.listen = function(cb){
this.use(this.router.connectMiddleware);
- this.server.listen(port, cb);
+ this.expressServer.listen(this.port, cb);
};
module.exports = Percolator;
View
5 resources/_index.js
@@ -0,0 +1,5 @@
+
+exports.handler = {
+ GET : function(req, res){ res.end("Hello World!"); }
+
+}
View
9 simplest.js
@@ -0,0 +1,9 @@
+var Percolator = require('./percolator');
+
+var server = new Percolator();
+server.router.route('/', { GET : function(req, res){
+ res.end("Hello World!");
+ }});
+server.listen(function(err){
+ console.log('server is listening on port ', server.port);
+});
View
27 test_server.js
@@ -18,30 +18,39 @@ var express = require('express');
var app = {
protocol : 'http',
- resourceDir : __dirname + '/test/test_fixtures/resources',
resourcePath : '/api',
staticDir : __dirname + '/static',
port : 8080
};
var server = new Percolator(app);
-
-server.use(express.favicon());
-server.use(express['static'](app.staticDir));
-server.use(express.bodyParser());
+server.use(express.bodyParser()); // TODO does this work for PUT?!?!
server.use(function(req, res, next){
console.log(req.method, ' ', req.url);
next();
});
-server.routeDirectory(app.resourceDir, function(err){
- console.log("routing resources in " + app.resourceDir);
+server.expressServer.get('/asdfasdf', function(req, res){
+ res.send("booyakabooyaka");
+});
+
+
+var resourceDir = __dirname + '/test/test_fixtures/resources';
+server.routeDirectory(resourceDir, function(err){
+ console.log("routed resources in " + resourceDir);
+
+ server.router.route('/inside',
+ { GET : function(req, res){
+ res.send("muahahah!");
+ }
+ }).as('inside');
+
if (err) {
console.log("Routing error");
console.log(err);
return;
}
- server.listen(app.port, function(err){
+ server.listen(function(err){
if (err) {console.log(err);throw err;}
- console.log('Percolator running on ' + app.port);
+ console.log('Percolator running on ' + server.port);
});
});
Please sign in to comment.
Something went wrong with that request. Please try again.