Permalink
Browse files

Merge ../../blog (finished)

Conflicts:
	app.js
	views/post/new.html
  • Loading branch information...
2 parents 3fc360b + bf07f07 commit 126ca21db9c3a2678710c262dd424057424eaa3a @croach committed Aug 13, 2012
Showing with 4,236 additions and 46 deletions.
  1. +3 −0 .gitignore
  2. +73 −25 app.js
  3. +33 −0 db.js
  4. +39 −0 httpUtils.js
  5. 0 node_modules/ejs/.gitmodules
  6. +4 −0 node_modules/ejs/.npmignore
  7. +114 −0 node_modules/ejs/History.md
  8. +24 −0 node_modules/ejs/Makefile
  9. +168 −0 node_modules/ejs/Readme.md
  10. +14 −0 node_modules/ejs/benchmark.js
  11. +581 −0 node_modules/ejs/ejs.js
  12. +1 −0 node_modules/ejs/ejs.min.js
  13. +24 −0 node_modules/ejs/examples/client.html
  14. +7 −0 node_modules/ejs/examples/list.ejs
  15. +14 −0 node_modules/ejs/examples/list.js
  16. +2 −0 node_modules/ejs/index.js
  17. +331 −0 node_modules/ejs/lib/ejs.js
  18. +198 −0 node_modules/ejs/lib/filters.js
  19. +23 −0 node_modules/ejs/lib/utils.js
  20. +28 −0 node_modules/ejs/package.json
  21. +176 −0 node_modules/ejs/support/compile.js
  22. +194 −0 node_modules/ejs/test/ejs.js
  23. +1 −0 node_modules/ejs/test/fixtures/backslash.ejs
  24. +1 −0 node_modules/ejs/test/fixtures/backslash.html
  25. +5 −0 node_modules/ejs/test/fixtures/comments.ejs
  26. +4 −0 node_modules/ejs/test/fixtures/comments.html
  27. +1 −0 node_modules/ejs/test/fixtures/double-quote.ejs
  28. +1 −0 node_modules/ejs/test/fixtures/double-quote.html
  29. +5 −0 node_modules/ejs/test/fixtures/error.ejs
  30. +8 −0 node_modules/ejs/test/fixtures/error.out
  31. +1 −0 node_modules/ejs/test/fixtures/include.css.ejs
  32. +3 −0 node_modules/ejs/test/fixtures/include.css.html
  33. +5 −0 node_modules/ejs/test/fixtures/include.ejs
  34. +9 −0 node_modules/ejs/test/fixtures/include.html
  35. +1 −0 node_modules/ejs/test/fixtures/includes/menu-item.ejs
  36. +1 −0 node_modules/ejs/test/fixtures/includes/menu/item.ejs
  37. +11 −0 node_modules/ejs/test/fixtures/menu.ejs
  38. +3 −0 node_modules/ejs/test/fixtures/menu.html
  39. +1 −0 node_modules/ejs/test/fixtures/messed.ejs
  40. +1 −0 node_modules/ejs/test/fixtures/messed.html
  41. +5 −0 node_modules/ejs/test/fixtures/newlines.ejs
  42. +9 −0 node_modules/ejs/test/fixtures/newlines.html
  43. +5 −0 node_modules/ejs/test/fixtures/no.newlines.ejs
  44. +5 −0 node_modules/ejs/test/fixtures/no.newlines.html
  45. +1 −0 node_modules/ejs/test/fixtures/para.ejs
  46. +1 −0 node_modules/ejs/test/fixtures/pet.ejs
  47. +1 −0 node_modules/ejs/test/fixtures/single-quote.ejs
  48. +1 −0 node_modules/ejs/test/fixtures/single-quote.html
  49. +3 −0 node_modules/ejs/test/fixtures/style.css
  50. +1 −0 node_modules/ejs/test/fixtures/user.ejs
  51. +19 −0 node_modules/mime/LICENSE
  52. +63 −0 node_modules/mime/README.md
  53. +104 −0 node_modules/mime/mime.js
  54. +34 −0 node_modules/mime/package.json
  55. +55 −0 node_modules/mime/test.js
  56. +1,588 −0 node_modules/mime/types/mime.types
  57. +59 −0 node_modules/mime/types/node.types
  58. +23 −0 public/css/master.css
  59. +52 −0 router.js
  60. +26 −0 views/post/edit.html
  61. +42 −0 views/post/list.html
  62. +14 −21 views/post/new.html
  63. +12 −0 views/post/show.html
View
@@ -0,0 +1,3 @@
+script.js
+*~
+\#*\#
View
98 app.js
@@ -1,31 +1,79 @@
-var http = require('http');
-var url = require('url');
-var fs = require('fs');
+var http = require('http'),
+ util = require('util'),
+ httpUtils = require('./httpUtils'),
+ Router = require('./router'),
+ DB = require('./db');
-var newPostFormHTML = fs.readFileSync('views/post/new.html');
+var blog = new DB();
+var router = new Router();
-function renderNewPostForm(request, response) {
- response.writeHead(200, {
- 'Content-type': 'text/html; charset=utf-8'
+// Add our routes
+
+// List all posts
+router.get('^/posts/?$', function(req, res) {
+ var options = {posts: blog.posts()};
+ httpUtils.renderHtml('post/list.html', res, options);
+});
+
+// Show a specific post
+router.get('^/posts/(\\d+)$', function(req, res, params) {
+ var post = blog.get(params[0]);
+ if (!post) httpUtils.render404(res);
+ var options = {locals: {post: post}};
+ httpUtils.renderHtml('post/show.html', res, options);
+})
+
+// Show the "New Post" form
+router.get('^/posts/new/?$', function(req, res) {
+ var options = {};
+ httpUtils.renderHtml('post/new.html', res, options);
+});
+
+// Add a new post
+router.post('^/posts/?$', function(req, res) {
+ httpUtils.parseBody(req, function(body) {
+ var post = {
+ title: body.title,
+ content: body.content
+ }
+ blog.add(post);
+ httpUtils.redirect('/posts', res);
});
- response.end(newPostFormHTML);
-}
-
-function render404(request, response) {
- response.writeHead(404);
- response.end('404 File not found');
-}
-
-var server = http.createServer(function(request, response) {
- var newPostFormRegex = new RegExp('^/posts/new/?$');
- var pathname = url.parse(request.url).pathname;
- if (newPostFormRegex.test(pathname)) {
- renderNewPostForm(request, response);
- } else {
- render404(request, response);
- }
});
-server.listen(8000);
+// Delete the post
+router.post('^/posts/(\\d+)/delete/?$', function(req, res, params) {
+ var id = params[0];
+ if (blog.get(id)) blog.remove(id);
+ httpUtils.redirect('/posts/', res);
+});
+
+// Show the "Update Post" form
+router.get('^/posts/(\\d+)/edit', function(req, res, params) {
+ var post = blog.get(params[0]);
+ if (!post) httpUtils.render404(res);
+ var options = {locals: {post: post}};
+ httpUtils.renderHtml('post/edit.html', res, options);
+});
+
+// Update the post
+router.post('^/posts/(\\d+)/edit', function(req, res, params) {
+ var id = params[0];
+ if (!blog.get(id)) httpUtils.render404(res);
+
+ httpUtils.parseBody(req, function(body) {
+ var post = {
+ title: body.title,
+ content: body.content
+ };
+ blog.update(id, post);
+ httpUtils.redirect('/posts', res);
+ });
+})
+
+// Create the actual http server
-console.log('Listening on http://127.0.0.1:8000');
+var server = http.createServer(function(req, res) {
+ router.dispatch(req, res);
+}).listen(8000);
+util.puts("Server running at http://127.0.0.1:8000/");
View
33 db.js
@@ -0,0 +1,33 @@
+function DB() {
+ this.db = {};
+}
+
+var id = 0;
+
+DB.prototype.add = function(obj) {
+ obj.id = ++id;
+ this.db[id] = obj;
+};
+
+DB.prototype.update = function(id, obj) {
+ obj.id = id;
+ this.db[id] = obj;
+};
+
+DB.prototype.remove = function(id) {
+ delete this.db[id];
+};
+
+DB.prototype.get = function(id) {
+ return this.db[id];
+};
+
+DB.prototype.posts = function() {
+ var posts = [];
+ for (var id in this.db) {
+ posts.push(this.db[id]);
+ }
+ return posts.sort(function(a, b) { return a.id - b.id; });
+};
+
+module.exports = DB;
View
@@ -0,0 +1,39 @@
+var qs = require('querystring'),
+ fs = require('fs'),
+ ejs = require('ejs' ),
+ path = require('path');
+
+exports.defaultViewsDir = path.join(__dirname, "views");
+
+exports.renderHtml = function(view, response, options, viewsDir) {
+ viewsDir = viewsDir || exports.defaultViewsDir;
+ var filepath = path.join(viewsDir, view);
+ fs.readFile(filepath, 'utf8', function(err, template) {
+ var html = ejs.render(template, options);
+ response.writeHead(200, {"Content-Type": "text/html"});
+ response.end(html);
+ });
+}
+
+exports.render404 = function(response) {
+ response.writeHead(404);
+ response.end("404 File not found");
+}
+
+exports.redirect = function(url, response) {
+ response.writeHead(302, {
+ 'Content-Type': 'text/html',
+ 'Location': url
+ });
+ response.end();
+}
+
+exports.parseBody = function(req, callback) {
+ var body = "";
+ req.on('data', function(chunk) {
+ body += chunk;
+ });
+ req.on('end', function() {
+ callback(qs.parse(body));
+ });
+}
No changes.

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

Oops, something went wrong.
View

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

Oops, something went wrong.
View

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

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

0 comments on commit 126ca21

Please sign in to comment.