Flatiron routing table with flatiron.app

283 Views Asked by At

I started flatiron from tutorial which defines flatiron.app:

app = flatiron.app;

Tutorial defines routing with methods get, post, etc.

app.router.get('/', function () {
   this.res.writeHead(200, { 'Content-Type': 'text/plain' });
   this.res.end('juuri\n');
});

Instead of this I would like to use director with routing tables

var routes = {
   '/': (function(){
      this.res.writeHead(200, { 'Content-Type': 'text/plain' });
      this.res.end('root\n');
    }),

   '/cat': (function(){
      this.res.writeHead(200, { 'Content-Type': 'text/plain' });
      this.res.end('meow\n');
    })
}; 

How to apply routing table this to "native" app.router?

Is it possible to separate get and post requests with routing table?

Ok, found answer from flatirons google group:

This works:

var routes = {
    //
    // a route which assigns the function `bark`.
    //
    '/': {
        get: (function(){
           this.res.writeHead(200, { 'Content-Type': 'text/plain'});
           this.res.end('root\n');
        })

    },

    '/cat': { 
          get: (function(){
             this.res.writeHead(200, { 'Content-Type': 'text/plain' });
             this.res.end('cat\n');
          })
        }
      }

app.router.mount(routes);This works:

var routes = {
    //
    // a route which assigns the function `bark`.
    //
    '/': {
        get: (function(){
           this.res.writeHead(200, { 'Content-Type': 'text/plain'});
           this.res.end('root\n');
        })

    },
    '/cat': { 
        get: (function(){
           this.res.writeHead(200, { 'Content-Type': 'text/plain' });
           this.res.end('cat\n');
        })
     }
}

app.router.mount(routes);
0

There are 0 best solutions below