Auto-adding pages with node.js on heroku?

It would be wonderful if you could help me with this! I'm using node.js and heroku to deply an app and I'm changing my web.js file everything I add a new post. This was fine earlier, but with more pages, it's starting to get out of hand. I'm wondering if I could automate this? Eg, this is what my web.js file looks like now:

var express = require('express');
var fs = require('fs');
var htmlfile = "index.html";
var app = express(express.logger());

//day 7 and so on.. till day 15.

var day6 = "./100/day6/day6.html";
app.get('/day6', function(request, response) {
var html = fs.readFileSync(day6).toString();

var day5 = "./100/day5/day5.html";
app.get('/day5', function(request, response) {
var html = fs.readFileSync(day5).toString();

var day4 = "./100/day4/day4.html";
app.get('/day4', function(request, response) {
var html = fs.readFileSync(day4).toString();

var day3 = "./100/day3/day3.html";
app.get('/day3', function(request, response) {
var html = fs.readFileSync(day3).toString();

var day2 = "./100/day2/day2.html";
app.get('/day2', function(request, response) {
var html = fs.readFileSync(day2).toString();

var challengehtmlfile = "100.html";
app.get('/100-day-challenge', function(request, response) {
var html = fs.readFileSync(challengehtmlfile).toString();

var bloghtmlfile = "blog.html";
app.get('/blog', function(request, response) {
var html = fs.readFileSync(bloghtmlfile).toString();

app.get('/', function(request, response) {
var html = fs.readFileSync(htmlfile).toString();

var port = process.env.PORT || 8080;
app.listen(port, function() {
  console.log("Listening on " + port);

Problem courtesy of: Kostar


If you are planning to add much more pages, it is not a good idea to add one route per page. Better way is to use pattern matching in your routing. For example,

app.get('/day/:num', function(request, response) {
var day = "./100/day" + request.params.num + "/day" + request.params.num + ".html";
var html = fs.readFileSync(day).toString();

Now this route would match any number of days. Only thing you would have to change is the URL. Instead of '/day3', you should access '/day/3'. In /day/:num , :num is just a variable name in the url. You access that variable through request.params.num. Then you construct the filename with that information. Hope this would clean up your script. :) For more info, check express api here ( ).

Solution courtesy of: lovetostrike


Copy this code and save it as "".


sed -i "/express(express.logger());/avar ${day} = \"./100/${day}/${day}.html\";\napp.get('/${day}', function(request, response) {\nvar html = fs.readFileSync(${day}).toString();\nresponse.send(html);\n});\n" $2

This script takes two parameters: # of days and file name. For example,

bash 3 your<strong>node.js</strong>

This command will insert the necessary code for day3 in your node.js file. I assumed you want to add those lines after express.logger() line. If you want to put the code after a different line, simply replace express(express.logger()); in the script to anything you want. Please let me know.

Discussion courtesy of: lovetostrike

This recipe can be found in it's original form on Stack Over Flow .