Use the new router

This commit is contained in:
John Crepezzi 2011-11-23 13:14:18 -05:00
parent 1dbaa54811
commit 167c7c4adb
4 changed files with 33 additions and 22 deletions

View file

@ -8,6 +8,9 @@
* Proper markdown highlighting * Proper markdown highlighting
* Better about page text * Better about page text
* test new interface in browsers * test new interface in browsers
* compress assets
* cache static assets in memory with connect
* look for a better way to do router
# shared version only # shared version only

View file

@ -7,7 +7,7 @@
"maxLength": 400000, "maxLength": 400000,
"cacheStaticAssets": false, "staticMaxAge": 86400,
"logging": [ "logging": [
{ {

View file

@ -19,7 +19,8 @@
"dependencies": { "dependencies": {
"winston": "*", "winston": "*",
"hashlib": "*" "hashlib": "*",
"connect": "*"
}, },
"devDependencies": { "devDependencies": {

View file

@ -3,8 +3,8 @@ var url = require('url');
var fs = require('fs'); var fs = require('fs');
var winston = require('winston'); var winston = require('winston');
var connect = require('connect');
var StaticHandler = require('./lib/static_handler');
var DocumentHandler = require('./lib/document_handler'); var DocumentHandler = require('./lib/document_handler');
// Load the configuration and set some defaults // Load the configuration and set some defaults
@ -52,9 +52,6 @@ for (var name in config.documents) {
}); });
} }
// Configure a static handler for the static files
var staticHandler = new StaticHandler('./static', !!config.cacheStaticAssets);
// Configure the document handler // Configure the document handler
var documentHandler = new DocumentHandler({ var documentHandler = new DocumentHandler({
store: preferredStore, store: preferredStore,
@ -62,21 +59,31 @@ var documentHandler = new DocumentHandler({
keyLength: config.keyLength keyLength: config.keyLength
}); });
// Set the server up and listen forever // Set the server up with a static cache
http.createServer(function(request, response) { connect.createServer(
var incoming = url.parse(request.url, false); connect.router(function(app) {
var handler = null; // add documents
// Looking to add a new doc app.post('/documents', function(request, response, next) {
if (incoming.pathname.match(/^\/documents$/) && request.method == 'POST') {
return documentHandler.handlePost(request, response); return documentHandler.handlePost(request, response);
} });
// Looking up a doc // get documents
var match = incoming.pathname.match(/^\/documents\/([A-Za-z0-9]+)$/); app.get('/documents/:id', function(request, response, next) {
if (request.method == 'GET' && match) { return documentHandler.handleGet(req.params.id, response);
return documentHandler.handleGet(match[1], response); });
} }),
// Otherwise, look for static file // Otherwise, static
staticHandler.handle(incoming.pathname, response); connect.staticCache(),
}).listen(config.port, config.host); connect.static(__dirname + '/static', { maxAge: config.staticMaxAge }),
// Then we can loop back - and change these into '/' TODO
connect.router(function(app) {
app.get('/:id', function(request, response, next) {
request.url = request.originalUrl = '/';
next();
});
}),
// Static
connect.staticCache(),
connect.static(__dirname + '/static', { maxAge: config.staticMaxAge })
).listen(config.port, config.host);
winston.info('listening on ' + config.host + ':' + config.port); winston.info('listening on ' + config.host + ':' + config.port);