Permalink
Browse files

convert example server to express for the routing

  • Loading branch information...
1 parent eb32a70 commit e295267b852fef8ba917492f45898f850a0c0ba8 @dqminh dqminh committed Dec 19, 2012
Showing with 41 additions and 45 deletions.
  1. +40 −45 bin/exampleserver
  2. +1 −0 package.json
View
@@ -6,59 +6,24 @@
// It demonstrates a few techniques to get different application behaviour.
require('coffee-script');
-var connect = require('connect'),
- sharejs = require('../src'),
- hat = require('hat').rack(32, 36);
+var express = require('express'),
+ sharejs = require('../src'),
+ hat = require('hat').rack(32, 36);
var argv = require('optimist').
- usage("Usage: $0 [-p portnum]").
- default('p', 8000).
- alias('p', 'port').
- argv;
+ usage("Usage: $0 [-p portnum]").
+ default('p', 8000).
+ alias('p', 'port').
+ argv;
-var server = connect(
- connect.favicon(),
- connect.static(__dirname + '/../examples'),
- connect.router(function (app) {
- var renderer = require('../examples/_static');
- app.get('/static/:docName', function(req, res, next) {
- var docName;
- docName = req.params.docName;
- renderer(docName, server.model, res, next);
- });
-
- var wiki = require('../examples/_wiki');
- app.get('/wiki/?', function(req, res, next) {
- res.writeHead(301, {location: '/wiki/Main'});
- res.end();
- });
-
- app.get('/wiki/:docName', function(req, res, next) {
- var docName;
- docName = req.params.docName;
- wiki(docName, server.model, res, next);
- });
-
- app.get('/pad/?', function(req, res, next) {
- var docName;
- docName = hat();
- res.writeHead(303, {location: '/pad/pad.html#' + docName});
- res.write('');
- res.end();
- });
-
- app.get('/?', function(req, res, next) {
- res.writeHead(302, {location: '/index.html'});
- res.end();
- });
- })
-);
+var server = express();
+server.use(express.static(__dirname + '/../examples'));
var options = {
db: {type: 'none'},
browserChannel: {cors: '*'},
auth: function(client, action) {
- // This auth handler rejects any ops bound for docs starting with 'readonly'.
+ // This auth handler rejects any ops bound for docs starting with 'readonly'.
if (action.name === 'submit op' && action.docName.match(/^readonly/)) {
action.reject();
} else {
@@ -81,6 +46,36 @@ var port = argv.p;
// Attach the sharejs REST and Socket.io interfaces to the server
sharejs.server.attach(server, options);
+var renderer = require('../examples/_static');
+server.get('/static/:docName', function(req, res, next) {
+ var docName;
+ docName = req.params.docName;
+ renderer(docName, server.model, res, next);
+});
+
+var wiki = require('../examples/_wiki');
+server.get('/wiki/?', function(req, res, next) {
+ res.writeHead(301, {location: '/wiki/Main'});
+ res.end();
+});
+server.get('/wiki/:docName', function(req, res, next) {
+ var docName;
+ docName = req.params.docName;
+ wiki(docName, server.model, res, next);
+});
+
+server.get('/pad/?', function(req, res, next) {
+ var docName;
+ docName = hat();
+ res.writeHead(303, {location: '/pad/pad.html#' + docName});
+ res.write('');
+ res.end();
+});
+
+server.get('/?', function(req, res, next) {
+ res.writeHead(302, {location: '/index.html'});
+ res.end();
+});
server.listen(port);
console.log("Demos running at http://localhost:" + port);
View
@@ -23,6 +23,7 @@
"hat": "*"
},
"devDependencies": {
+ "express": "~ 3.x",
"nodeunit": "*",
"websocket": "*"
},

0 comments on commit e295267

Please sign in to comment.