Free personal site hosting with Heroku and Node.js
So, I finally set out to make my personal website. I really didn't want to spend the $7 a month or whatever it is just to have some static page hosted, especially since I also had to pay for the domain name (I'm pretty cheap if you couldn't tell). The solution? Heroku.
Heroku isn't for static pages, in fact it doesn't really let you set up just a simple index.html file and push it. Luckily, it does support Node.js, and it's really simple to write a Node server to display static content. Here's how.
First you are going to need a site to host. I'm not going to show you how to do that here, but at least have an index.html file. Any files that are linked in your HTML should go into a folder (I called mine 'resources'). When you link your files do not do './resources/file.ext', instead just do 'file.ext', Node will handle the directory for you. Now create a new file to store your server code in, I called mine web.js
Copy and paste the following code into that file:
// web.js // this file comes from heroku's node setup document var express = require('express');
var app = express.createServer(express.logger());
app.use(express.static(__dirname + '/resources'));
app.get('/', function(request, response) { response.sendfile('./index.html'); });
var port = process.env.PORT || 5000; app.listen(port, function() { console.log("Listening on " + port); });
Now add the necessary dependencies for npm (node package manager) into a file called pacakge.json.
// package.json // This file is copied from heroku's node setup document. { "name": "node-example", "version": "0.0.1", "dependencies": { "express": "2.5.x" }, "engines": { "node": "0.8.x", "npm": "1.1.x" } }
Now type the following into your command line in the same directory as your website folder:
heroku login npm install touch README.md git init git add . git commit -m "init" heroku create git push heroku master
And that's it! You should now be able to type in heroku open to launch your website on the web! To use your own domain name, follow this guide from Heroku to set up your DNS forwarding.
T














