Skip to content

Commit bd274ff

Browse files
committed
Added the first route and view
1 parent 5e2b6ac commit bd274ff

File tree

3 files changed

+68
-7
lines changed

3 files changed

+68
-7
lines changed

README.md

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,19 @@
11
Node.js Blog Engine
22
===================
33

4-
This is the source code for the Nettuts+ series on web development with Node.js.
4+
This is the source code for the Nettuts+ series on web development
5+
with Node.js.
56

6-
### v0.0
7-
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.
7+
### part-01
8+
9+
The code for the second episode in the series. In this episode a first
10+
route and view were added as well as some basic directory structure.
11+
12+
### part-00
13+
14+
This is the code for the first part of the series. This was
15+
essentially an introduction to Node and the code is a simple Hello
16+
World example.
817

918
License
1019
=======
@@ -25,4 +34,4 @@ This work is published from:
2534
<span property="vcard:Country" datatype="dct:ISO3166"
2635
content="US" about="http://christopherroach.com">
2736
United States</span>.
28-
</p>
37+
</p>

app.js

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,29 @@
11
var http = require('http');
2+
var url = require('url');
3+
var fs = require('fs');
24

3-
var server = http.createServer(function(request, response) {
5+
var newPostFormHTML = fs.readFileSync('views/post/new.html');
6+
7+
function renderNewPostForm(request, response) {
48
response.writeHead(200, {
5-
'Content-type': 'text/plain'
9+
'Content-type': 'text/html; charset=utf-8'
610
});
7-
response.end('Hello World');
11+
response.end(newPostFormHTML);
12+
}
13+
14+
function render404(request, response) {
15+
response.writeHead(404);
16+
response.end('404 File not found');
17+
}
18+
19+
var server = http.createServer(function(request, response) {
20+
var newPostFormRegex = new RegExp('^/posts/new/?$');
21+
var pathname = url.parse(request.url).pathname;
22+
if (newPostFormRegex.test(pathname)) {
23+
renderNewPostForm(request, response);
24+
} else {
25+
render404(request, response);
26+
}
827
});
928

1029
server.listen(8000);

views/post/new.html

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
2+
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
3+
<html xmlns="http://www.w3.org/1999/xhtml">
4+
<head>
5+
<title>
6+
Missing Title
7+
</title>
8+
<link href="/css/master.css" rel="stylesheet" type="text/css" />
9+
</head>
10+
<body>
11+
<h1>
12+
New post
13+
</h1>
14+
<form method="post" action="/posts" id="new_post" class="new_post">
15+
<div class="field">
16+
<label for="post_title">Title</label><br />
17+
<input type="text" name="title" id="post_title" size="30" />
18+
</div>
19+
<div class="field">
20+
<label for="post_content">Content</label><br />
21+
<textarea name="content" cols="40" rows="20" id="post_content">
22+
</textarea>
23+
</div>
24+
<div class="actions">
25+
<input type="submit" value="Create Post" id="post_submit" />
26+
</div>
27+
</form>
28+
<p>
29+
<br />
30+
<a href="/posts">Back</a>
31+
</p>
32+
</body>
33+
</html>

0 commit comments

Comments
 (0)