Devin Rhode
Devin Rhode

Reputation: 25287

Setup file-system based routing in Node

I'm really a fan of the simplicity PHP offers in serving pages, everything is based on the filesystem. I want to do the same thing with Node. I tried one routing setup that worked like this for views, but broke my public folder:

//using express:
app.get('*', function(req, res) {
  file = req.params[0].substr(1, req.params[0].length);
  console.log('requesting: ' + file);
  res.render(file, {locals: {
    req: req,
    params: req.query
  }});
});

So...

What's the best way to setup filesystem based/php style routing in Node?

Upvotes: 1

Views: 1040

Answers (2)

Gates VP
Gates VP

Reputation: 45287

I think that I build exactly what you are looking for. I use this to serve up .jade files, obviously you can tweak this for your use case.

var url = require('url');
var express = require('express');
var app = express.createServer();
var fs = require('fs');

app.set("view engine", "jade");

app.use(app.router);
app.use(express.static(__dirname + '/public'));

/**
 * Generic "get" attempts to route to known JADE files.
 * If no known JADE files, then we pass routing to next() (should be static).
 */
app.get('*', function(req, res, next) {

  var pathname = url.parse(req.url).pathname.toLowerCase(); // make matching case insenstive

  // First case: with no path name, render the default index.jade
  if(!pathname) {
    res.render('index', {});
  }
  // Second case: path ending in '/' points to a folder, use index.jade from that folder
  else if (pathname === '/' || pathname.charAt(pathname.length-1) === '/' ){
    res.render(__dirname + '/views' + pathname + 'index.jade', {});
  }
  // Third case: looks like an actual file, attempt to render
  else {
    // Attempt to find the referenced jade file and render that. Note 'views' is default path.
    fs.stat( (__dirname + "/views" + pathname + '.jade'), function(err, stats){
      // There was an error, the file does not exist pass control to the static handler
      if(err || !stats) {
        next();
      }
      // We found the file, render it.
      else{
        res.render(pathname.substring(1), {});
      }
    });

  }
});

app.listen(port);

Note, there should be more app.use() statements there for handling cookies, parsing the body etc. Also, the second param of the render is always empty. You may want to fill this out with things like {layout: xyz} or generic variables that need to come in to the rendered pages.

Upvotes: 2

250R
250R

Reputation: 37151

You can use express.static()

For examples:

app.configure(function(){
  app.use(express.static(__dirname + '/public'));
});

app.configure(function(){
  app.use('/uploads', express.static(PATH_TO_UPLOAD_FOLDER));
});

Upvotes: 0

Related Questions