Permalink
Browse files

First check in. Very basic setup in place

  • Loading branch information...
0 parents commit c85b62f3830ca8c0f2b4a24d99b49da830d18c8b @hectorcorrea committed Apr 10, 2012
Showing with 276 additions and 0 deletions.
  1. +1 −0 .gitignore
  2. +8 −0 README
  3. +42 −0 app.js
  4. +1 −0 data/blog.1.html
  5. +1 −0 data/blog.2.html
  6. +18 −0 data/blogs.json
  7. +9 −0 package.json
  8. +8 −0 public/stylesheets/style.css
  9. +76 −0 routes/blog.js
  10. +40 −0 routes/index.bak
  11. +3 −0 routes/index.js
  12. +13 −0 sandbox/readjson.js
  13. +12 −0 sandbox/test.json
  14. +3 −0 views/404.jade
  15. +5 −0 views/about.jade
  16. +10 −0 views/blog.jade
  17. +10 −0 views/blogs.jade
  18. +8 −0 views/index.jade
  19. +6 −0 views/layout.jade
  20. +2 −0 views/page2.jade
@@ -0,0 +1 @@
+node_modules/
8 README
@@ -0,0 +1,8 @@
+A very simple blog web site using Node.js and Express.js
+
+This is a rough work in progress.
+
+At this point there is code to handle a home page, an about page, a way to see a list of topics, and click on each topic to view the details of the post.
+
+The data is kept in text files for now.
+
42 app.js
@@ -0,0 +1,42 @@
+
+/**
+ * Module dependencies.
+ */
+
+var express = require('express');
+var routes = require('./routes'); // aka ./routes/index.js
+
+var app = module.exports = express.createServer();
+
+// Configuration
+app.configure(function(){
+ app.set('views', __dirname + '/views');
+ app.set('datapath', __dirname + '/data');
+ app.set('view engine', 'jade');
+ app.use(express.bodyParser());
+ app.use(express.methodOverride());
+ app.use(app.router);
+ app.use(express.static(__dirname + '/public'));
+});
+
+app.configure('development', function(){
+ app.use(express.errorHandler({ dumpExceptions: true, showStack: true }));
+});
+
+app.configure('production', function(){
+ app.use(express.errorHandler());
+});
+
+// Routes
+app.get('/', routes.blog.index);
+app.get('/about', routes.blog.about);
+app.get('/blog/:id?', routes.blog.blog);
+
+app.get('*', function(req, res){
+ res.render('404.jade',
+ { status: 404, message: 'Page not found' });
+});
+
+app.listen(3000, function(){
+ console.log("Express server listening on port %d in %s mode", app.address().port, app.settings.env);
+});
@@ -0,0 +1 @@
+<p>This is the <b>first</b> blog post</p>
@@ -0,0 +1 @@
+<p>This is the <b>second</b> blog post</p>
@@ -0,0 +1,18 @@
+{
+ "blogs": [
+ {
+ "id": 1,
+ "title": "first blog post",
+ "url": "first-blog-post",
+ "file": "blog.1.html",
+ "summary": "Where we talk about the creation of the universe",
+ "postedOn": "2011-10-28 19:30" },
+ {
+ "id": 2,
+ "title": "second blog post",
+ "url": "second-blog-post",
+ "file": "blog.2.html",
+ "summary": "Talking about dinosaurs",
+ "postedOn": "2012-04-08 22:10" }
+ ]
+}
@@ -0,0 +1,9 @@
+{
+ "name": "application-name"
+ , "version": "0.0.1"
+ , "private": true
+ , "dependencies": {
+ "express": "2.5.8"
+ , "jade": ">= 0.0.1"
+ }
+}
@@ -0,0 +1,8 @@
+body {
+ padding: 50px;
+ font: 14px "Lucida Grande", Helvetica, Arial, sans-serif;
+}
+
+a {
+ color: #00B7FF;
+}
@@ -0,0 +1,76 @@
+var fs = require("fs"); // file system
+
+// Blog "controller"
+index = function(req, res) {
+ var viewModel = { xxx: 'Home Page'};
+ res.render('index', viewModel);
+}
+
+about = function(req, res) {
+ var viewModel = { yyy: 'About'};
+ res.render('about', viewModel);
+}
+
+blog = function(req, res) {
+ if (req.params.id == undefined) {
+ blogAllTopics(req, res);
+ } else {
+ blogSingleTopic(req, res);
+ }
+}
+
+blogAllTopics = function(req, res) {
+
+ var dataPath = res.app.settings.datapath;
+ var filePath = dataPath + '/blogs.json';
+ console.log("Reading list of topics: " + filePath);
+
+ fs.readFile(filePath, 'utf8', function(err, text){
+
+ if(err != undefined)
+ {
+ console.log(err);
+ }
+
+ var viewModel = JSON.parse(text);
+ res.render('blogs', viewModel);
+ });
+
+ // var viewModel = {
+ // blogs: [
+ // {id: 1, title:"AAA", summary:"aaa"},
+ // {id: 2, title:"BBB", summary:"bbb"},
+ // {id: 3, title:"CCC", summary:"ccc"}]
+ // };
+ // res.render('blogs', viewModel);
+}
+
+blogSingleTopic = function(req, res) {
+
+ var id = req.params.id; //TODO: Force to number
+ var dataPath = res.app.settings.datapath;
+ var filePath = dataPath + '/blog.' + id + '.html';
+ console.log("Reading file: " + filePath);
+
+ fs.readFile(filePath, 'utf8', function(err, text){
+ if(err != undefined)
+ {
+ // TODO: redirect to topic not found
+ console.log(err);
+ text = "<p>topic not found</p>";
+ }
+ var viewModel = {
+ title: 'blog topic' + req.params.id,
+ content: text,
+ postedOn: "April 3rd, 2012"
+ };
+ res.render('blog', viewModel);
+ });
+
+}
+
+module.exports = {
+ index: index,
+ about: about,
+ blog: blog
+};
@@ -0,0 +1,40 @@
+
+/*
+ * GET home page.
+ */
+
+exports.index = function(req, res){
+ var viewModel = { xxx: 'Home Page'};
+ res.render('index', viewModel);
+};
+
+exports.about = function(req, res){
+ var viewModel = { yyy: 'About'};
+ res.render('about', viewModel);
+};
+
+exports.blog = function(req, res){
+ var viewModel = null;
+
+
+ if (req.params.id == undefined) {
+
+ viewModel = {
+ blogs: [
+ {title:"AAA", summary:"aaa"},
+ {title:"BBB", summary:"bbb"},
+ {title:"CCC", summary:"ccc"}]
+ };
+ res.render('blogs', viewModel);
+
+ } else {
+
+ viewModel = {
+ title: 'blog topic' + req.params.id,
+ content: "blah blah blah blah",
+ postedOn: "April 3rd, 2012"
+ };
+
+ res.render('blog', viewModel);
+ }
+};
@@ -0,0 +1,3 @@
+module.exports = {
+ blog: require('./blog')
+};
@@ -0,0 +1,13 @@
+var fs = require('fs');
+var file = __dirname + '/test.json';
+
+fs.readFile(file, 'utf8', function (err, data) {
+ if (err) {
+ console.log('Error: ' + err);
+ return;
+ }
+
+ data = JSON.parse(data);
+
+ console.dir(data);
+});
@@ -0,0 +1,12 @@
+{
+ "topics": [
+ { "id": 1,
+ "title": "first blog post",
+ "content": "blah 111 blah blah"
+ },
+ { "id": 2,
+ "title": "second blog post",
+ "content": "blah 222 blah blah"
+ }
+ ]
+}
@@ -0,0 +1,3 @@
+h1 Page not found
+p Sorry, the page that you are looking for does not exist.
+
@@ -0,0 +1,5 @@
+h1 About
+p This is the about page of the web site
+
+p
+ a(href='./') Home Page
@@ -0,0 +1,10 @@
+h1 Blog
+p Random posts about software development
+
+p #{title}
+
+!{content}
+
+p Posted on #{postedOn}
+p
+ a(href='/') Home Page
@@ -0,0 +1,10 @@
+h1 Blogs
+p Random posts about software development
+
+ul#blogs
+ each blog in blogs
+ li
+ a(href='/blog/#{blog.id}') #{blog.title} #{blog.summary}
+
+p
+ a(href='/') Home Page
@@ -0,0 +1,8 @@
+h1 Hello World
+p Welcome to Hello World in Express
+
+p
+ a(href='./about') About
+
+p
+ a(href='./blog') Blog
@@ -0,0 +1,6 @@
+!!!
+html
+ head
+ title Express Hello World
+ link(rel='stylesheet', href='/stylesheets/style.css')
+ body!= body
@@ -0,0 +1,2 @@
+h1 Page 2
+p Welcome to Page 2

0 comments on commit c85b62f

Please sign in to comment.