Add routing to Meteor JS

July 20, 2013

Reading time ~1 minute

Metor JS does not come with support for routing (i.e. having separate pages) out-of-the-box. But it is easy to add that using the router smart package.

Install the router smart package

Assuming that you have Meteorite installed. Just run:

mrt add router

Configure routes in your client JavaScript

Update your client JavaScript code to also contain the following:

/**
* Routing
*/
Meteor.Router.add({
  '/example': 'my-example',
  '/': 'welcome',
  '*': '404'
});

Update your HTML template

Now, we do not want to render all templates every time the page is loaded. It’s easy to fix my updating your templates slightly with the renderPage function.

<head>
  <title>Example service</title>
</head>
<body>
  
  
</body>

<template name="menu">
  <div class="menu">
    <strong>Example Service</strong>
    <a href="/">Home</a>
    <a href="/example">Example page</a>
  </div>
</template>

<template name="welcome">
  <p>This is the start page</p>
</template>

<template name="my-example">
  <h1>My example</h1>
  <p>This is an example page</p>
</template>

<template name="404">
  <h1>Oops, page not found</h1>
  <p>The page you were looking for has gone missing!</p>
</template>

More features available

This is just a basic example of how to use the router smart package. Check out the project’s webpage for detailed documentation.

Running vim-airline with Maximum Awesome

I use [Maximum Awesome](https://github.com/square/maximum-awesome) when I code in [vim](http://www.vim.org/), it's a great starting place...… Continue reading

Quicksort implementation in Python

Published on August 04, 2014

Fibonacci generator in Python

Published on August 04, 2014