Permalink
Browse files

[examples] Added server examples.

  • Loading branch information...
1 parent f06a7a8 commit 8c645ab7cf7346e9b6bfb1493e777afb6b6a5a5d @Marak Marak committed Jun 19, 2012
Showing with 61 additions and 44 deletions.
  1. +10 −0 examples/multi-server.js
  2. +51 −44 examples/server.js
@@ -0,0 +1,10 @@
+var formful = require('../lib/formful');
+var addons = require('addons');
+var resourceful = require('resourceful');
+
+var Database = resourceful.define('database', addons.schemas.database);
+var Dropbox = resourceful.define('dropbox', addons.schemas.dropbox);
+
+formful.createServer([Database, Dropbox]).listen(8000, function () {
+ console.log(' > formful server started on port 8000');
+});
View
@@ -1,50 +1,57 @@
-var http = require('http'),
- formful = require('../lib/formful'),
- director = require('director'),
- resourceful = require('resourceful'),
- restful = require('restful');
-
-
-var router = new director.http.Router();
+var formful = require('../lib/formful')
+var resourceful = require('resourceful');
var Creature = resourceful.define('creature', function () {
- this.string('diet');
-});
-var formfulRouter = formful.createRouter(Creature);
-var restfulRouter = restful.createRouter(Creature);
-
-//
-// Override the default restful response handlers,
-// to use the formfulRouter instead
-//
-
-restful.extendRouter(router, Creature, {}, function (req, res, status, key, value) {
- formfulRouter.dispatch(req, res, function (err) {
- if (err) {
- if (res.statusCode === 201) {
- res.write(302);
- res.writeHead('Location', '/creatures');
- }
- res.end();
- }
- })
-});
+ this.string('diet', { required: true });
+
+ //
+ // A required email address
+ //
+ this.string('email', { format: 'email', description: 'Enter email...', message: "Invalid email!", required: true });
+
+ //
+ // Assume an array of strings for now...
+ //
+ this.string('flavor', { enum: ['cherry', 'lemon', 'purple'] });
+
+ /*
+ this.timestamps();
+
+ //
+ // Remark: All custom type validation is provided by `revalidator`
+ // see: https://github.com/flatiron/revalidator
+
+
+ //
+ // An optional homepage with a default value
+ //
+ this.string('homepage', { format: 'url', default: "http://monster.com" });
+
+ //
+ // An ip address
+ //
+ this.string('ip', { format: 'ip-address' });
+
+ //
+ // An date-time address
+ //
+ this.string('date-time', { format: 'date-time' });
+
+ //
+ // An ipv6 address
+ //
+ this.string('date', { format: 'date' });
+
+ //
+ // A color
+ //
+ this.string('color', { format: 'color' });
+
+ */
-var server = http.createServer(function (req, res) {
- req.chunks = [];
- req.on('data', function (chunk) {
- req.chunks.push(chunk.toString());
- });
- router.dispatch(req, res, function (err) {
- if (err) {
- formfulRouter.dispatch(req, res, function (err) {
- res.writeHead(404);
- res.end();
- })
- }
- });
});
-server.listen(8000);
-console.log(' > formful server started on port 8000');
+formful.createServer(Creature).listen(8000, function () {
+ console.log(' > formful server started on port 8000');
+});

0 comments on commit 8c645ab

Please sign in to comment.