0

In response to the comment by AutumnLeonard here, I tried a minimalistic implementation of the idea. I first added the iron router package via "meteor add iron:router" and then tried this code:

blogtest.html:

<head>
  <title>blogtest</title>
</head>

<body>
  <h1>This is really something, isn't it!?!</h1>

  {{> thought}}
  {{> anotherthought}}
</body>

<template name="thought">
  <p>THis is a random thought.</p>
</template>

<template name="anotherthought">
  <p>THis is another random thought.</p>
</template>

blogtest.js:

Router.route("/:blog_post_title", {template: "thought", name: "thought"});
Router.route("/:blog_post_title", {template: "anotherthought", name: "anotherthought"});

if (Meteor.isClient) {
  // counter starts at 0
  Session.setDefault('counter', 0);

  Template.hello.helpers({
    counter: function () {
      return Session.get('counter');
    }
  });

  Template.hello.events({
    'click button': function () {
      // increment the counter when button is clicked
      Session.set('counter', Session.get('counter') + 1);
    }
  });
}

if (Meteor.isServer) {
  Meteor.startup(function () {
    // code to run on server at startup
  });
}

(the top two lines are the only ones I added; the rest are superfluous but harmless "boilerplate" code left over from the default meteor app)

...but on trying to run it, it fails with throbbing blue and purple growlings emanating from the command prompt, to wit:

W20151007-09:25:00.634(-7)? (STDERR) Error: A route for the path "/:blog_post_ti
tle" already exists by the name of "anotherthought".

(why does it complain about "anotherthought" but not about "another" if my IronRouter syntax is wrong here?)

W20151007-09:25:00.635(-7)? (STDERR)     at blogtest.js:2:8

(line 2, char 8 is the "r" in "Router.route" on the second line...???)

W20151007-09:25:00.635(-7)? (STDERR)     at C:\Misc\blogtest\.meteor\local\build
\programs\server\app\blogtest.js:37:4

(there is no line 37 in blogtest.js ...???)

UPDATE

Okay, so I changed the HTML to:

<head>
  <title>blogtest</title>
</head>

<body>
  <h1>Here's a thought:</h1>

  <!-- {{> thought}}
  {{> anotherthought}} -->
</body>

<template name="thought">
  <p>This is a random thought.</p>
</template>

<template name="anotherthought">
  <p>This is another random thought.</p>
</template>

...and the routing code to:

Router.route("/thought", {template: "thought", name: "thought"});
Router.route("/anotherthought", {template: "anotherthought", name: "anotherthought"});

...and it no longer fails to run; in fact, I do see what I would expect to when I enter "http://localhost:3000/thought", namely:

Here's a thought:

This is a random thought.

...and what I expect with "http://localhost:3000/anotherthought", too, namely:

Here's a thought:

This is another random thought.

However, I see this at localhost:3000 (the default URL):

Here's a thought:

Oops, looks like there's no route on the client or the server for url: "http://localhost:3000/."

So what do I need to enter so that the "Oops" goes away? What Route.route() is needed?

Community
  • 1
  • 1
B. Clay Shannon-B. Crow Raven
  • 8,547
  • 144
  • 472
  • 862

3 Answers3

2

you define two same route ("/:blog_post_title"), cannot be same I guess. Maybe you can try to change one of them. maybe another one you can define as "/:blog_post_title2".

just my 2 cents

cddin
  • 33
  • 5
1

You do not sound happy:-( Meteor is a bit of a learning curve, but for me it's been very worth it.

I see a couple of things here that might be tripping you up.

Templates vs Routes: The difference between a route and a template.

A template is like a recipe for filling in some HTML. Nesting templates in templates helps to break an app into smaller pieces.

A route is like erasing everything and starting over. You essentially drop all the HTML, and start with a new template.

This difference comes from how web apps were built before, and is still very useful now.

Template Includes: you wouldn't use a route like this: {{> thought }}. That's the syntax for including a template.
This would import HTML templates (like you're defining). You don't need a route to make this work.

Routes: Here, routes are defining the top template. They wipe everything (with exceptions like Session variables) and start over.

The path is important because it identifies the place in the app. This let's users bookmark places in the app.

Having two routes with the same path is an error for sure. Which should be used for the bookmark? Delete one of the routes to move forward.

Body: You can't stuff things in the body like you're doing at the top of your HTML. (Well you can, but it's not best practices:-) Meteor basically appends the routes template to the tag. It's was jarring to not define a <body>, but that's how it works.

Change the <body> to <template name="main">, and fix the </body>

Then add the template to a route:

Router.route("/", {template: "main"});

This might not work 100%, but it should get you past some of these blocks you're having.

Also, relax and have fun! After coming from PHP and Angular/Express, Meteor is a lot of fun!

You might try the Discover Meteor book. It was a great way for me to get started. Just took a couple days to get started.

Mogsdad
  • 44,709
  • 21
  • 151
  • 275
Michael Cole
  • 15,473
  • 7
  • 79
  • 96
  • It's not me that's cranky, it's Meteor; and I only used that word because StackOverflow doesn't allow the word "problem," which it apparently sees as problematic. Please see my update - it works as expected now, except when going to the "raw" URL (localhost:3000) – B. Clay Shannon-B. Crow Raven Oct 07 '15 at 17:26
  • 1
    No problme :-) It's just the impression I got :-) Sure. There's no route with a path "/", so Iron Router is telling you that. If you set `Router.route("/", {template: "thought", name: "thought"}); `, it should load at '/'. [These guys](http://meteortips.com/second-meteor-tutorial/iron-router-part-1/) and the [docs](https://iron-meteor.github.io/iron-router/) are way better at explaining it than me :-) – Michael Cole Oct 07 '15 at 17:43
0

It turns out that it's easier than what I was trying; most of that fancy-pants stuff was effluvia.

All I need in the *.js file is:

Router.route('/');
Router.route('/thought');
Router.route('/anotherthought');

And the following HTML works as expected (showing just the H1 for "localhost:3000", and showing the appropriate template in addition to the H1 when I append "/thought" or "/anotherthought" to that base URL.

<head>
  <title>blogtest</title>
</head>

<body>
  <h1>Here's a thought:</h1>
</body>

<template name="thought">
  <p>This is a random thought.</p>
</template>

<template name="anotherthought">
  <p>This is another random thought.</p>
</template>

So to create a meteor app to which I can add content that I can share, all I need to do is:

0) At the command prompt, enter "meteor create <projectName>", such as: "meteor create thoughts"

1) After cd'ing to the directory (which is the projectName), as directed, enter "meteor add iron:router"

2) So that your "raw" URL doesn't throw an iron exception, add this to your .js file:

Router.route('/');

3) Add a new template to the .html file each time I want to make something public.

4) Then (each time), add a routing directive in the .js file with the name of the template; e.g., if you add this template:

<template name="thought">
  <p>This is a random thought.</p>
</template>

...you would add this line to the .js file:

Router.route('/thought');

5) Make your meteor app public by, at the command prompt, entering "meteor deploy "; e.g., you could enter "meteor deploy thoughts" or "meteor deploy rompecabeza" (the deployed name doesn't have to match the project name).

6) Advise whoever you want to see it to "point their browser at" your URL, such as, "Hey, gang! Go czech out '<your URL, with the appended template name>'! You can give me a penny later!"

That's all there is to it.

For example, I created a static site that serves up a few of my photos. They can be seen via these links:

http://dplatypus.meteor.com/pinnacles
http://dplatypus.meteor.com/garrapatabeach
http://dplatypus.meteor.com/garrapataturnout
http://dplatypus.meteor.com/mcwayfalls
http://dplatypus.meteor.com/pfeifferbeach

Alternatively, you can just click the links above from dplatypus.meteor.com

B. Clay Shannon-B. Crow Raven
  • 8,547
  • 144
  • 472
  • 862