Use node.js without the tynnary of express


Using node.js without the tyranny of express and jade.

For some reason I've still been obsessed with node.js, and you can read about my first impressions in my previous blog post here. I'm not quite sure why I've had this little love affair with node.js; after all it's an interpreted language that uses JavaScript on the server - something I'm not quite sure even makes sense. My buddies tell me I shouldn't continue this torrid affair with node.js and that my one true love .NET is waiting for me at home. Everyone is correct. However, but for those of you who have a twitch and want to play with node.js a little, one of the things you may be having a hard time with is getting to know node.js's best friend. Express. In fact, all the walkthroughs I've been on just assume you're using express and they make sure you have it installed. I was annoyed. BUT THEN I discovered one of the very best websites I've been to that really describes the process of getting node.js up and running. http://nodebeginner.org/. This site goes step by step in showing how the whole process works from beginning to end. Starts with just getting it to display hello world, and then walks though the process of creating routers, the server scripts, everything. Once you're done you can *really* see what node.js is doing when it's doing what it's doing.

Except for how to display HTML pages.

Now toward the end of the walkthrough, there are HTML pages being displayed but it's basically in-line code. Not something you're going to want to do in a real word situation. It looks like this:

function start(response) {
  
console.log("Request handler 'start' was called.");
  
var body = '<html>'
   +
 '<head>'
   +
'<meta http-equiv="Content-Type" content="text/html; '
  
+ 'charset=UTF-8" />'
  
+ '</head>'
  
+ '<body>'
  
+ '<form action="/upload" method="post">'
  
+ '<textarea name="text" rows="20" cols="60"></textarea>'
  
+ '<input type="submit" value="Submit text" />'
  
+ '</form>'
  
+ '</body>'
  
+ '</html>';
  
response.writeHead(200, {"Content-Type": "text/html"});
  
response.write(body);
  
response.end();
}

Now, Express will handle this all for you. Basically you can create a views folder, put your jade formatted markup in there, and express will display it for you. Jade? Oh yeah, that's a friend of Express who tags along. Thus far, I haven't been able to find a way to say 'Hey, Express... can we do our thing without Jade hanging around? Can we just have our fun with HTML?' Apparently not. So with all that then, there is a way to just display HTML pages. Make sure you're including fs.

fs = require('fs');
 
Then, you can display it like so:

function start(response) {
  
console.log("Request handler 'start' was called.");
  
var filename = 'view/start.html';
  
fs.readFile(filename, "binary", function(err, file) {
     
if(err) {
        
response.writeHead(500, {"Content-Type": "text/plain"});
        
response.write(err + "\n");
        
response.close();
        
return;
     
}

      response.writeHead(200, {"Content-Type": "text/html"});
     
response.write(file, "binary");
     
response.end();
     
});
}

So.... tada! Now, you can use clientside JavaScript, and all the other static-like HTML you want to use!

Now, in all honesty, you should be using some sort of engine. Without it, you are missing out on a lot of work that a lot of people have already done for you. The ability to inject content server side, for instance is helpful in creating powerful sites using node js. The github page for node lists all kind of frameworks you can use instead of Express if you prefer. So if your like me and you want to do as much as you can without a framework, and then grumble and start using one, hopefully this information is helpful.

Comments (0)

Skip to main content