进阶路由

页面丢失

If a user types an incorrect URL, chances are you want to show them some kind of amusing not found page. There are actually two categories of "not found" pages. The first is when the URL typed in doesn't match any of your route definitions. You can use FlowRouter.notFound to handle this:

// the App_notFound template is used for unknown routes and missing lists
FlowRouter.notFound = {
  action() {
    BlazeLayout.render('App_body', {main: 'App_notFound'});
  }
};

The second is when the URL is valid, but doesn't actually match any data. In this case, the URL matches a route, but once the route has successfully subscribed, it discovers there is no data. It usually makes sense in this case for the page component (which subscribes and fetches the data) to render a not found template instead of the usual template for the page:

<template name="Lists_show_page">
  {{#each listId in listIdArray}}
    {{> Lists_show (listArgs listId)}}
  {{else}}
    {{> App_notFound}}
  {{/each}}
<template>

Analytics

It's common to want to know which pages of your app are most commonly visited, and where users are coming from. You can read about how to set up Flow Router based analytics in the Deployment Guide.

服务端路由

As we've discussed, Meteor is a framework for client rendered applications, but this doesn't always remove the requirement for server rendered routes. There are two main use cases for server-side routing.

Server Routing for API access

Although Meteor allows you to write low-level connect handlers to create any kind of API you like on the server-side, if you all you want to do is create a RESTful version of your Methods and Publications, you can often use the simple:rest package to do this easily. See the Data Loading and Methods articles for more information.

If you need more control, you can use the comprehensive nimble:restivus package to create more or less whatever you need in whatever ontology you require.

服务端渲染

Blaze UI库并不提供服务端渲染的功能,所以当你使用Blaze时,你是无法在服务器上渲染你的网页的。可是,React UI库却可以实现这些。这意味着当你把React作为渲染框架时,在服务器上渲染HTML成为了可能。

Although Flow Router can be used to render React components more or less as we've described above for Blaze, at the time of this writing Flow Router's support for SSR is still experimental. However, it's probably the best approach right now if you want to use SSR for Meteor. 尽管如我们之前针对Blaze描述的,Flow Route或多或少地被用作渲染React组件,当前编写Flow Router提供的SSR(Server-Side Rending)仍是试验性的。不过,如果你想在Meteor中使用SSR,这或许仍是当前最好的方法。

results matching ""

    No results matching ""