Skip to content

Commit

Permalink
convert example server to express for the routing
Browse files Browse the repository at this point in the history
  • Loading branch information
dqminh committed Dec 20, 2012
1 parent eb32a70 commit e295267
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 45 deletions.
85 changes: 40 additions & 45 deletions bin/exampleserver
Expand Up @@ -6,59 +6,24 @@
// It demonstrates a few techniques to get different application behaviour. // It demonstrates a few techniques to get different application behaviour.


require('coffee-script'); require('coffee-script');
var connect = require('connect'), var express = require('express'),
sharejs = require('../src'), sharejs = require('../src'),
hat = require('hat').rack(32, 36); hat = require('hat').rack(32, 36);


var argv = require('optimist'). var argv = require('optimist').
usage("Usage: $0 [-p portnum]"). usage("Usage: $0 [-p portnum]").
default('p', 8000). default('p', 8000).
alias('p', 'port'). alias('p', 'port').
argv; argv;


var server = connect( var server = express();
connect.favicon(), server.use(express.static(__dirname + '/../examples'));
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 options = { var options = {
db: {type: 'none'}, db: {type: 'none'},
browserChannel: {cors: '*'}, browserChannel: {cors: '*'},
auth: function(client, action) { 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/)) { if (action.name === 'submit op' && action.docName.match(/^readonly/)) {
action.reject(); action.reject();
} else { } else {
Expand All @@ -81,6 +46,36 @@ var port = argv.p;
// Attach the sharejs REST and Socket.io interfaces to the server // Attach the sharejs REST and Socket.io interfaces to the server
sharejs.server.attach(server, options); 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); server.listen(port);
console.log("Demos running at http://localhost:" + port); console.log("Demos running at http://localhost:" + port);


Expand Down
1 change: 1 addition & 0 deletions package.json
Expand Up @@ -23,6 +23,7 @@
"hat": "*" "hat": "*"
}, },
"devDependencies": { "devDependencies": {
"express": "~ 3.x",
"nodeunit": "*", "nodeunit": "*",
"websocket": "*" "websocket": "*"
}, },
Expand Down

0 comments on commit e295267

Please sign in to comment.