Created
April 21, 2014 22:22
-
-
Save laser/11158619 to your computer and use it in GitHub Desktop.
Barrister RPC + Express
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Server: Barrister RPC + Express | |
var express = require('express') | |
, fs = require('fs') | |
, http = require('http') | |
, path = require('path') | |
, barrister = require('barrister') | |
, store = require("./store").store | |
, idl = JSON.parse(fs.readFileSync("./todo_manager.json").toString()); | |
var app = express(); | |
app.use(express.bodyParser()); | |
app.use(express.methodOverride()); | |
app.use(app.router); | |
var server = new barrister.Server(idl); | |
server.addHandler('TodoManager', { | |
'readTodos': function(callback) { | |
callback(null, store.getAll()); | |
}, | |
'createTodo': function(props, callback) { | |
callback(null, store.save(props)); | |
}, | |
'updateTodo': function(id, props, callback) { | |
callback(null, store.update(id, props)); | |
}, | |
'deleteTodo': function(id, callback) { | |
callback(null, null); | |
} | |
}); | |
app.post('/todos', function(req, res) { | |
server.handle({}, req.body, function(respJson) { | |
res.contentType('application/json'); | |
res.send(respJson); | |
}); | |
}); | |
http.createServer(app).listen(3000, function(){ | |
console.log('Express server listening on port ' + app.get('port')); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment