Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

updated example1

  • Loading branch information...
commit 51e2df5970bbaf9702b7162a7c647142b108638a 1 parent 002806c
@rsms authored
View
67 examples/example1/README.md
@@ -0,0 +1,67 @@
+# Example 1
+
+This is a simple introductory example which uses no authentication nor sessions, but do expose a server API as well as a client.
+
+Example 1 is a service which stores messages which can be any JSON object.
+
+Start the server like this:
+
+ node server.js --debug
+
+*The `--debug` argument will cause the server to print information about what it's doing.*
+
+## Server API
+
+A good way to explore and play with Oui server APIs is to use [cURL](http://curl.haxx.se/). See further down for a sample cURL session.
+
+All server code in this example lives in `server.js`.
+
+
+### GET /messages -> {id:message, ..}
+
+Retrieve all messages. Returns a map `id => message`.
+
+### POST message -> /messages -> {id:int}
+
+Add a new message. Returns the new message id.
+
+### GET /messages/:id -> {id:int, message:object}
+
+Retrieve a specific message with id `:id`. Returns a message.
+
+### PUT|POST message -> /messages/:id -> {message:object}
+
+Assign a message to the id `:id`. Returns any message previously assigned to `:id`.
+
+### DELETE /messages/:id -> {message:object}
+
+Remove a message with id `:id`. Returns the message which was deleted.
+
+### POST /messages/:id/delete -> {message:object}
+
+Like `DELETE /messages/:id -> message` but implemented as POST to support old browsers (which are unable to send DELETE requests).
+
+
+## Sample cURL session
+
+ $ curl localhost:8080/messages
+ {}
+ $ curl -d '{"message":{"title":"Hello"}}' \
+ -H 'content-type: application/json' localhost:8080/messages
+ {"id":1}
+ $ curl localhost:8080/messages
+ {"1":{"title":"Hello"}}
+ $ curl -d '{"message":{"title":"Hej", "language":"sv"}}' \
+ -H 'content-type: application/json' localhost:8080/messages
+ {"id":2}
+ $ curl localhost:8080/messages
+ {"1":{"title":"Hello"},"2":{"title":"Hej","language":"sv"}}
+ $ curl -X DELETE localhost:8080/messages/1
+ {"message":{"title":"Hello"}}
+ $ curl localhost:8080/messages
+ {"2":{"title":"Hej","language":"es"}}
+ $ curl -X PUT -d '{"message":{"title":"Hola", "language":"es"}}' \
+ -H 'content-type: application/json' localhost:8080/messages/2
+ {"message":{"title":"Hej","language":"sv_SE"}}
+ $ curl localhost:8080/messages
+ {"2":{"title":"Hola","language":"es"}}
View
21 examples/example1/client/message-board.html
@@ -1,28 +1,11 @@
<script type="text/javascript" charset="utf-8">
oui.app.on('start', function(ev){
- exports.pipe = oui.pipe.get('message-board', function(err){
- if (err) throw err;
- __html.find('form').removeClass('closed');
- });
- exports.pipe.on('message', function(ev, msg){
- if (msg.title && msg.message)
- __html.find('ul.messages').prepend(
- '<li>'+
- '<b>'+oui.htmlesc(msg.title)+':</b> '+
- oui.htmlesc(msg.message)+
- '</li>'
- );
- })
- .on('error', function(ev, err){
- console.error(__name+'.pipe error: '+(err ? err.stack || err : ''));
- })
- .on('close', function(ev, err){
- __html.find('form').addClass('closed');
- });
+ console.log('start')
});
$(function(){
+ console.log('dom')
var form = __html.find('form');
form.submit(function(ev){
var msg = {};
View
71 examples/example1/server.js
@@ -1,18 +1,73 @@
var fs = require('fs'),
path = require('path'),
- oui = require('oui');
+ oui = require('../../oui');
// Start a server
var server = oui.server.start({
port: 8080,
- documentRoot: path.join(path.dirname(fs.realpathSync(__filename)), 'public'),
- allowedOrigin: /^https?:\/\/(?:(?:.+\.|)(?:dropular|hunch)\.[^\.]+|localhost|.+\.local)(?::[0-9]*|)$/,
+ documentRoot: path.dirname(fs.realpathSync(__filename)) + '/public',
+ authSecret: 'you should change this in a real app',
});
-// Enable standard functionality (static file handling, sessions, etc).
-server.enableStandardHandlers();
+// In this example we store our messages in memory. In a real world app you
+// would probably save these persistently.
+var messages = {
+ map: {},
+ nextId: 1,
+ add: function(message) {
+ var id = messages.nextId++;
+ messages.map[id] = message;
+ return id;
+ }
+};
-// A custom request handler saying hello
-server.on('GET', '/hello', function(){
- return 'Hello to you too sir';
+// read all messages
+server.GET('/messages', {onlyKeys:'boolean'}, function(params, req, res){
+ if (params.onlyKeys) {
+ return Object.keys(messages.map);
+ } else {
+ return messages.map;
+ }
});
+
+// add a message
+server.POST('/messages', {message:{required:true}}, function(params, req, res) {
+ return {id: messages.add(params.message)};
+});
+
+// get a specific message
+server.GET('/messages/:id', {id:'int'}, function(params, req, res) {
+ // retrieve message
+ var message = messages.map[params.id];
+ // if message was not found, send a 404
+ if (!message)
+ return res.send(404, 'wtf');
+ // send the message and it's id to the client
+ return {id: params.id, message: message};
+});
+
+// update a specific message (also allow POST so old web browser can use this)
+server.handle(['PUT', 'POST'], '/messages/:id', {
+ id: 'int',
+ message: {required: true}
+}, function(params, req, res) {
+ var message = messages.map[params.id];
+ messages.map[params.id] = params.message;
+ // send the previous message to the client
+ return {message: message};
+});
+
+// remove a specific message
+function removeHandler(params, req, res) {
+ var message = messages.map[params.id];
+ if (!message)
+ return res.send(404);
+ messages.map[params.id] = undefined;
+ // send the old message to the client
+ return {message: message};
+}
+var argspec = {id:{type:'int'}};
+// DELETE is the "correct" way...
+server.DELETE('/messages/:id', argspec, removeHandler);
+// ...but old browsers only support GET and POST
+server.POST('/messages/:id/delete', argspec, removeHandler);
Please sign in to comment.
Something went wrong with that request. Please try again.