Permalink
Browse files

added examples, closes #13

  • Loading branch information...
1 parent ab63897 commit fd94d7fcf5ab1a0dce9e34f9d5b30735356c98d7 @kilianc committed Jun 2, 2012
Showing with 5,095 additions and 0 deletions.
  1. +2 −0 .gitignore
  2. +6 −0 examples/account/config/routes.json
  3. +1 −0 examples/account/lib/api_modules/index.js
  4. +109 −0 examples/account/lib/api_modules/users.js
  5. +13 −0 examples/account/package.json
  6. +9 −0 examples/account/public/css/bootstrap.min.css
  7. +4 −0 examples/account/public/css/main.css
  8. BIN examples/account/public/img/glyphicons-halflings-white.png
  9. BIN examples/account/public/img/glyphicons-halflings.png
  10. +59 −0 examples/account/public/index.html
  11. +1 −0 examples/account/public/js/async.min.js
  12. +1,550 −0 examples/account/public/js/handlebars-1.0.0.beta.6.js
  13. +98 −0 examples/account/public/js/main.js
  14. +36 −0 examples/account/public/js/mootools-more-1.4.0.1.js
  15. +527 −0 examples/account/public/js/mootools-yui-compressed.js
  16. +83 −0 examples/account/public/js/utils.js
  17. +10 −0 examples/account/public/partials/user_row.html
  18. +65 −0 examples/account/server.js
  19. +4 −0 examples/instagram/config/routes.json
  20. +1 −0 examples/instagram/lib/api_modules/index.js
  21. +113 −0 examples/instagram/lib/api_modules/photos.js
  22. +13 −0 examples/instagram/package.json
  23. +9 −0 examples/instagram/public/css/bootstrap.min.css
  24. +2 −0 examples/instagram/public/css/main.css
  25. BIN examples/instagram/public/img/glyphicons-halflings-white.png
  26. BIN examples/instagram/public/img/glyphicons-halflings.png
  27. +39 −0 examples/instagram/public/index.html
  28. +1 −0 examples/instagram/public/js/async.min.js
  29. +1,550 −0 examples/instagram/public/js/handlebars-1.0.0.beta.6.js
  30. +59 −0 examples/instagram/public/js/main.js
  31. +36 −0 examples/instagram/public/js/mootools-more-1.4.0.1.js
  32. +527 −0 examples/instagram/public/js/mootools-yui-compressed.js
  33. +83 −0 examples/instagram/public/js/utils.js
  34. +15 −0 examples/instagram/public/partials/photo.html
  35. +70 −0 examples/instagram/server.js
View
@@ -0,0 +1,2 @@
+examples/instagram/public/uploads/
+examples/instagram/tmp/
@@ -0,0 +1,6 @@
+[
+ ["/signin", "1/users#signin"],
+ ["/signup", "1/users#signup"],
+ ["/list/:limit/:page", "1/users#list", { "limit": 20, "page": 1 }],
+ ["/users/:id", "1/users#get"]
+]
@@ -0,0 +1 @@
+exports.Users = require("./users")
@@ -0,0 +1,109 @@
+var ObjectId = require('mongodb').ObjectID
+
+var Users = module.exports = function (options) {
+ var self = this
+ options = (options !== null && options !== undefined && options.constructor === Object) ? options : {}
+ Object.keys(options).forEach(function (key) {
+ if (!self.__proto__.hasOwnProperty(key)) {
+ self[key] = options[key]
+ }
+ })
+}
+
+Users.prototype.signup = {
+ post: function (request, response) {
+ var self = this
+ request.resume()
+ request.once('end', function () {
+ if (!self._isEmail(request.body.email)) {
+ response.serveJSON({ success: false, error: 'invalid email ' + request.body.email })
+ return
+ }
+ if (!self._isPassword(request.body.password)) {
+ response.serveJSON({ success: false, error: 'invalid password' })
+ return
+ }
+ self.collections.users.save({
+ email: request.body.email,
+ password: request.body.password
+ }, function (err, document) {
+ response.serveJSON({ success: true, user: document })
+ })
+ })
+ }
+}
+
+Users.prototype.signin = {
+ post: function (request, response) {
+ var self = this
+ request.resume()
+ request.once('end', function () {
+ self.collections.users.findOne({ email: request.body.email, password: request.body.password }, function (err, document) {
+ if (err) {
+ response.serveJSON({ success: false, error: err.message }, { httpStatusCode: 404 })
+ return
+ }
+ if (!document) {
+ response.serveJSON({ success: false, error: 'user not found' }, { httpStatusCode: 404 })
+ return
+ }
+ response.serveJSON({ success: true }, {
+ headers: {
+ 'set-cookie': 'session_id=' + document._id.toHexString()
+ }
+ })
+ })
+ })
+ }
+}
+
+Users.prototype.list = {
+ get: function (request, response) {
+ var limit = Number(request.querystring.limit)
+ var page = Number(request.querystring.page)
+ var stream = this.collections.users.find({}, { limit: limit, skip: (page-1) * limit }).stream()
+ stream.on('data', function(item) {
+ response.streamJSON(item)
+ })
+ stream.on('close', function() {
+ response.streamJSON()
+ })
+ }
+}
+
+Users.prototype.get = {
+ get: function (request, response) {
+ this.collections.users.findOne({ _id: new ObjectId(request.querystring.id) }, function (err, document) {
+ if (err || !document) {
+ response.serveJSON({ success: false, error: err.message })
+ return
+ }
+ response.serveJSON({ success: true, user: document })
+ })
+ }
+}
+
+Users.prototype.delete = {
+ post: function (request, response) {
+ var self = this
+ request.resume()
+ request.once('end', function () {
+ self.collections.users.remove({ _id: new ObjectId(request.body.id) }, { safe: true }, function (err, document) {
+ if (err) {
+ response.serveJSON({ success: false, error: err.message })
+ return
+ }
+ response.serveJSON({ success: true, id: request.body.id })
+ })
+ })
+ }
+}
+Users.prototype.delete.delete = Users.prototype.delete.post
+
+Users.prototype._isEmail = function (email) {
+ return /^([a-z0-9_\.\-])+\@(([a-z0-9\-])+\.)+([a-z0-9]{2,4})+$/.test(email)
+}
+
+Users.prototype._isPassword = function (email) {
+ return /^.{6,22}$/.test(email)
+}
@@ -0,0 +1,13 @@
+{
+ "name": "apiserver-example",
+ "version": "0.0.0",
+ "private": true,
+ "dependencies": {
+ "console-trace": "https://github.com/LearnBoost/console-trace/tarball/master",
+ "apiserver": "https://github.com/kilianc/node-apiserver/tarball/new-router",
+ "mongodb": "1.0.x",
+ "request": "2.9.x",
+ "connect": "2.3.x",
+ "colors": ""
+ }
+}

Large diffs are not rendered by default.

Oops, something went wrong.
@@ -0,0 +1,4 @@
+iframe { display: none }
+section { padding-top: 5px }
+.btn-success { margin-right: 10px }
+.btn-info { margin-right: 10px }
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
@@ -0,0 +1,59 @@
+<!doctype html>
+<html>
+<head>
+ <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
+ <link rel="stylesheet" href="css/bootstrap.min.css" type="text/css" media="screen">
+ <link rel="stylesheet" href="css/main.css" type="text/css" media="screen">
+ <script src="js/mootools-yui-compressed.js"></script>
+ <script src="js/mootools-more-1.4.0.1.js"></script>
+ <script src="js/handlebars-1.0.0.beta.6.js"></script>
+ <script src="js/async.min.js" type="text/javascript" charset="utf-8"></script>
+ <script src="js/utils.js" type="text/javascript" charset="utf-8"></script>
+ <script src="js/main.js?account"></script>
+ <title>ApiServer Examples / account</title>
+</head>
+<body>
+ <body>
+ <div class="container">
+ <div class="page-header">
+ <h1>ApiServer Example / <small>Basic accounts management</small></h1>
+ </div>
+ <section id="section-signup">
+ <form id="signup-form" class="form-vertical" action="http://localhost:8080/signup" method="post">
+ <fieldset>
+ <legend>Signup</legend>
+ <div class="control-group">
+ <label class="control-label" for="signup-email">Email</label>
+ <div class="controls">
+ <input type="text" id="signup-email" class="input-xlarge" name="email" placeholder="you@example.com">
+ </div>
+ <label class="control-label" for="signup-password">Password</label>
+ <div class="controls">
+ <input type="password" id="signup-password" class="input-xlarge" name="password" placeholder="password">
+ </div>
+ </div>
+ </fieldset>
+ <button type="submit" class="btn btn-info">Signup</button>
+ </form>
+ </section>
+ <section id="section-list">
+ <div class="page-header">
+ <h1>Users / <small>http://localhost:8080/list</small></h1>
+ </div>
+ <table id="table-list" class="table table-striped">
+ <thead>
+ <tr>
+ <th>id</th>
+ <th>email</th>
+ <th>password</th>
+ <th></th>
+ </tr>
+ </thead>
+ <tbody>
+ </tbody>
+ </table>
+ </section>
+ </div>
+ </body>
+</body>
+</html>

Some generated files are not rendered by default. Learn more.

Oops, something went wrong.
Oops, something went wrong.

0 comments on commit fd94d7f

Please sign in to comment.