Permalink
Browse files

Added the first route and view

  • Loading branch information...
1 parent 5e2b6ac commit bd274ffdae41d258c475a3f81ec0c2bfdd9cc67d @croach committed Jun 13, 2011
Showing with 68 additions and 7 deletions.
  1. +13 −4 README.md
  2. +22 −3 app.js
  3. +33 −0 views/post/new.html
View
@@ -1,10 +1,19 @@
Node.js Blog Engine
===================
-This is the source code for the Nettuts+ series on web development with Node.js.
+This is the source code for the Nettuts+ series on web development
+with Node.js.
-### v0.0
-This is the code for the first part of the series. This was essentially an introduction to Node and the code is a simple Hello World example.
+### part-01
+
+The code for the second episode in the series. In this episode a first
+route and view were added as well as some basic directory structure.
+
+### part-00
+
+This is the code for the first part of the series. This was
+essentially an introduction to Node and the code is a simple Hello
+World example.
License
=======
@@ -25,4 +34,4 @@ This work is published from:
<span property="vcard:Country" datatype="dct:ISO3166"
content="US" about="http://christopherroach.com">
United States</span>.
-</p>
+</p>
View
25 app.js
@@ -1,10 +1,29 @@
var http = require('http');
+var url = require('url');
+var fs = require('fs');
-var server = http.createServer(function(request, response) {
+var newPostFormHTML = fs.readFileSync('views/post/new.html');
+
+function renderNewPostForm(request, response) {
response.writeHead(200, {
- 'Content-type': 'text/plain'
+ 'Content-type': 'text/html; charset=utf-8'
});
- response.end('Hello World');
+ 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);
View
@@ -0,0 +1,33 @@
+<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
+ "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
+<html xmlns="http://www.w3.org/1999/xhtml">
+ <head>
+ <title>
+ Missing Title
+ </title>
+ <link href="/css/master.css" rel="stylesheet" type="text/css" />
+ </head>
+ <body>
+ <h1>
+ New post
+ </h1>
+ <form method="post" action="/posts" id="new_post" class="new_post">
+ <div class="field">
+ <label for="post_title">Title</label><br />
+ <input type="text" name="title" id="post_title" size="30" />
+ </div>
+ <div class="field">
+ <label for="post_content">Content</label><br />
+ <textarea name="content" cols="40" rows="20" id="post_content">
+ </textarea>
+ </div>
+ <div class="actions">
+ <input type="submit" value="Create Post" id="post_submit" />
+ </div>
+ </form>
+ <p>
+ <br />
+ <a href="/posts">Back</a>
+ </p>
+ </body>
+</html>

0 comments on commit bd274ff

Please sign in to comment.