//import express module and create an express application
var express = require('express');
var fs=require('fs'); //import file system module
var app = express();

//sets the port for the server
app.set('port', (process.env.PORT || 5000));

//serving files in 'public' directory
app.use(express.static(__dirname + '/public'));

// views is directory for all template files
app.set('views', __dirname + '/views');
app.set('view engine', 'ejs');

//defines a route for the path of '/' relative to the site route
app.get('/', function(request, response) {
    var dir='views/posts/'; //directory is the posts folder
    var posts=[];
	var counter = 0; //starts with no posts
	fs.readdir(dir, function(err, files) { //reads files from directory
    	if (err) throw err; //error handling
    	files.forEach(function(file) {
			if (file.indexOf('.ejs', file.length - 4) === -1) { //if not an .ejs file
				return;
			}
			if (file === "template.ejs") { //if it is a template file
				return;
			}
			if (file === "hello_world.ejs") {
				return;			
			}
			counter++; //increase posts count
        	fs.readFile(dir + file, 'utf-8', function(err, html) { //for each file
            	if (err) throw err; //handle errors
				file = "/posts/" + file + "/"; //file path
				var lines = html.split('\n');
				var heading = lines[0].slice(4); //take off first 4 characters (<h3>)
				var a = heading.length;
				heading = heading.slice(0, a-5); //take off last 5 chars (</h3>)
				html = lines[1];
				var i = 2;	
				if (lines[i].length > 2){			
					while (lines[i].slice(4) == "") { //if line is not a html tag
						i = i + 1;
						html = lines[i]; }
				}
				if (html.length > 200){ //cut string to 200 chars
					html = html.slice(0, 200); }			
				html = html + "...";
           	 	posts.push({title: heading, content: html, link: file}); //add the blog post to the array of posts
				counter--;
				if (counter === 0) { //when gone through all posts, render the posts using the index.ejs template
					response.render('pages/index', {posts: posts}); //creates homepage
				}
        	});	
    	});
	});
});

//creates a route handler for the blog pages
app.get('/posts/:post/', function(request, response) {
	var path = 'views/posts/' + request.params.post; //path is the path of the post in the parameter
	fs.readFile(path, 'utf-8', function(err, data) {
		if (err) throw err;	
    	response.render('posts/template', {data: data}); //render (create) the post using the template
	});
});

//starts up server on a certain port
app.listen(app.get('port'), function() {
  console.log('Node app is running on port', app.get('port'));
});





@@ -1210,17 +1210,16 @@
 rors%0A%09%09%09
-%09
 file = %22
@@ -1253,17 +1253,16 @@
 le path%0A
-%09
 %09%09%09var l
@@ -1285,25 +1285,24 @@
 t('%5Cn');%0A%09%09%09
-%09
 var heading 
@@ -1359,17 +1359,16 @@
  (%3Ch3%3E)%0A
-%09
 %09%09%09var a
@@ -1389,17 +1389,16 @@
 gth;%0A%09%09%09
-%09
 heading 
@@ -1453,25 +1453,24 @@
  (%3C/h3%3E)%0A%09%09%09
-%09
 html = lines
@@ -1477,17 +1477,16 @@
 %5B1%5D;%0A%09%09%09
-%09
 var i = 
@@ -1489,17 +1489,16 @@
 i = 2;%09%0A
-%09
 %09%09%09if (l
@@ -1525,17 +1525,16 @@
 %09%09%09%0A%09%09%09%09
-%09
 while (l
@@ -1592,17 +1592,16 @@
 ag%0A%09%09%09%09%09
-%09
 i = i + 
@@ -1603,17 +1603,16 @@
  i + 1;%0A
-%09
 %09%09%09%09%09htm
@@ -1631,17 +1631,16 @@
 ; %7D%0A
-%09
 %09%09%09%7D%0A%09%09%09
 %09if 
@@ -1631,25 +1631,24 @@
 ; %7D%0A%09%09%09%7D%0A%09%09%09
-%09
 if (html.len
@@ -1685,25 +1685,24 @@
 0 chars%0A%09%09%09%09
-%09
 html = html.
@@ -1721,17 +1721,16 @@
 ); %7D%09%09%09%0A
-%09
 %09%09%09html 
@@ -2186,16 +2186,16 @@
 onse) %7B%0A
-
 %09var pat
@@ -2199,16 +2199,17 @@
 path = '
+/
 views/po

Create index.js