diff --git a/hn-server-fetch.js b/hn-server-fetch.js
index 3872e61..ead1f76 100644
--- a/hn-server-fetch.js
+++ b/hn-server-fetch.js
@@ -1,17 +1,22 @@
require('isomorphic-fetch')
+var ejs = require('ejs')
+var path = require('path')
+var fs = require('fs')
+
+var viewsPath = path.join('src', 'views')
/*
The official Firebase API (https://github.com/HackerNews/API) requires multiple network
connections to be made in order to fetch the list of Top Stories (indices) and then the
summary content of these stories. Directly requesting these resources makes server-side
-rendering cumbersome as it is slow and ultimately requires that you maintain your own
-cache to ensure full server renders are efficient.
+rendering cumbersome as it is slow and ultimately requires that you maintain your own
+cache to ensure full server renders are efficient.
To work around this problem, we can use one of the unofficial Hacker News APIs, specifically
-https://github.com/cheeaun/node-hnapi which directly returns the Top Stories and can cache
+https://github.com/cheeaun/node-hnapi which directly returns the Top Stories and can cache
responses for 10 minutes. In ReactHN, we can use the unofficial API for a static server-side
-render and then 'hydrate' this once our components have mounted to display the real-time
-experience.
+render and then 'hydrate' this once our components have mounted to display the real-time
+experience.
The benefit of this is clients loading up the app that are on flakey networks (or lie-fi)
can still get a fast render of content before the rest of our JavaScript bundle is loaded.
@@ -21,53 +26,20 @@ can still get a fast render of content before the rest of our JavaScript bundle
* Fetch top stories
*/
exports.fetchNews = function(page) {
- page = page || ''
- return fetch('http://node-hnapi.herokuapp.com/news' + page).then(function(response) {
- return response.json()
- }).then(function(json) {
- var stories = '
'
- json.forEach(function(data, index) {
- var story = '- ' +
- '' +
- ''
- '
'
- stories += story
- })
- stories += '
'
- return stories
- })
-}
-
-function renderNestedComment(data) {
- return ''
-}
-
-function generateNestedCommentString(data) {
- var output = ''
- data.comments.forEach(function(comment) {
- output+= renderNestedComment(comment)
- if (comment.comments) {
- output+= generateNestedCommentString(comment)
- }
- })
- return output
+ page = page || ''
+ return fetch('http://node-hnapi.herokuapp.com/news' + page)
+ .then(function(response) {
+ return response.json()
+ })
+ .then(function(stories) {
+ const pathToFile = path.join(viewsPath, 'news.ejs')
+ const templateStr = fs.readFileSync(pathToFile, 'utf8')
+ return ejs.render(
+ templateStr,
+ { stories: stories },
+ { filename: pathToFile }
+ )
+ })
}
/**
@@ -75,26 +47,17 @@ function generateNestedCommentString(data) {
* TODO: Add article summary at top of nested comment thread
*/
exports.fetchItem = function(itemId) {
- return fetch('https://node-hnapi.herokuapp.com/item/' + itemId).then(function(response) {
- return response.json()
- }).then(function(json) {
- var comments = ''
- json.comments.forEach(function(data, index) {
- var comment = '' +
- ''
- comments += generateNestedCommentString(data) + '
' + comment
- })
- return comments
- })
-}
\ No newline at end of file
+ return fetch('https://node-hnapi.herokuapp.com/item/' + itemId)
+ .then(function(response) {
+ return response.json()
+ })
+ .then(function(json) {
+ const pathToFile = path.join(viewsPath, 'comments.ejs')
+ const templateStr = fs.readFileSync(pathToFile, 'utf8')
+ return ejs.render(
+ templateStr,
+ { comments: json.comments, level: 0 },
+ { filename: pathToFile }
+ )
+ })
+}
diff --git a/src/views/comments.ejs b/src/views/comments.ejs
new file mode 100644
index 0000000..fb7babf
--- /dev/null
+++ b/src/views/comments.ejs
@@ -0,0 +1,4 @@
+<% comments.forEach(function(comment) { %>
+ <%- include("comments/comment", {comment, level: level + 1}) %>
+ <%- include("comments", {comments: comment.comments, level: level + 1}) %>
+<% }) %>
diff --git a/src/views/comments/comment.ejs b/src/views/comments/comment.ejs
new file mode 100644
index 0000000..3360941
--- /dev/null
+++ b/src/views/comments/comment.ejs
@@ -0,0 +1,17 @@
+
+
diff --git a/src/views/news.ejs b/src/views/news.ejs
new file mode 100644
index 0000000..34ff4dc
--- /dev/null
+++ b/src/views/news.ejs
@@ -0,0 +1,5 @@
+
+ <% stories.forEach(function(story) { %>
+ <%- include("news/story", {story: story}) %>
+ <% }) %>
+
diff --git a/src/views/news/story.ejs b/src/views/news/story.ejs
new file mode 100644
index 0000000..73b394b
--- /dev/null
+++ b/src/views/news/story.ejs
@@ -0,0 +1,11 @@
+
+
+
+