Node.js Is this a great way to show pages in a minimal way as possible? I'm trying to make a Node.js server as minimal as possible to show a landing page, but i'm not sure this is the greatest and fastest way to do it.

I'm using dirty to store my html data.

My app.js file

var http = require('http')
, db = require('dirty')('pages.db')
, url = require('url')
, fs = require('fs');

http.createServer(function (request, response) {
    var pathname = url.parse(request.url).pathname;
    var html = db.get(pathname);

    //Check if the requsted file is CSS or JS
    if (/\.(css)$/.test(pathname) || /\.(js)$/.test(pathname)){

        fs.createReadStream(__dirname + pathname, {
            'bufferSize': 4 * 1024
        }).pipe(response)

    } else if (!!html &amp;&amp; pathname !== '/admin' ) {
        //Pages from our Dirty DB
        response.writeHead(200, {'Content-Type': 'text/html'});
        response.end(html);
    } else if (pathname === '/admin') {
        //Display Admin page
        response.writeHead(200, {'Content-Type': 'text/html'});
        response.end('Admin!');
    } else {
        //Show 404 Page
        response.writeHead(404, {'Content-Type': 'text/html'});
        response.end(db.get('404'));
    }
 }).listen(80);

console.log('Server running at http://192.168.56.101:80/');
Good question,
made me learn some more about dirty.

Some observations:


1 var block is good, having the commas not at the end of each line looks odd
I wonder if it is faster to get the extension of the requested resource and compare to js and css instead of executing 2 regexes
Your 3 if blocks contain the same statement: response.writeHead(200, {'Content-Type': 'text/html'}); you should extract that statement into 1 place.
'80' should be in a var which you then use for your last console.log statement
Similarly, hard coding the ip address in your log statement does not make sense, you should get that info from node.
Always calculating 4*1024 and creating an options object seems like a lot work, that ought to get done only once
*


I would counter propose something like this:

var http = require('http'),
    db = require('dirty')('pages.db'),
    url = require('url'),
    fs = require('fs'),
    port = 80,
    streamOptions = { 'bufferSize': 4 * 1024 },
    htmlHeader = {'Content-Type': 'text/html'};

http.createServer(function (request, response)
{
  var pathname = url.parse(request.url).pathname;
  var html = db.get(pathname);

  //First check if the requsted file is css or js
  if (/\.(css)$/.test(pathname) || /\.(js)$/.test(pathname))
  {
    fs.createReadStream(__dirname + pathname,streamOptions).pipe(response);
  } 
  else 
  {
    response.writeHead(200, htmlHeader );
    if (!!html &amp;&amp; pathname !== '/admin' ) 
    { //Pages from our Dirty DB 
      response.end(html); 
    } 
    else if (pathname === '/admin') 
    { //Display Admin page
      response.end('Admin!'); 
    } 
    else 
    { //Not found..
      response.end(db.get('404'));
    }
  }
 }).listen(port);

console.log('Server running on port' , port );


If you absolutely want to show the IP address, then you should check out this excellent question.

Finally, any hacker can access your admin pages with /./admin tricks unless either dirty or url is smarter than I give it credit for.