Meteor:路由(1)

news/2024/5/5 3:03:56

Meteor本身并没有好的路由使用规则,还好有比较好用的第三方Package使用。Iron.Router就是一个比较好的Meteor的路由框架,提供了Client 和 Server的路由设置,还可以用来开发restful风格的api。

安装Iron.Router

直接cd到项目目录使用安装命令:

meteor add iron:router

Changes to your project's package version selections:

iron:controller        added, version 1.0.12
iron:core              added, version 1.0.11
iron:dynamic-template  added, version 1.0.12
iron:layout            added, version 1.0.12
iron:location          added, version 1.0.11
iron:middleware-stack  added, version 1.0.11
iron:router            added, version 1.0.12
iron:url               added, version 1.0.11


iron:router: Routing specifically designed for Meteor

可以看到安装的详细内容。
可以使用命令直接更新Iron.Router

meteor update iron:router

使用Iron.Router

我们知道使用meteor create meteor-project的命令创建的项目会默认生成三个文件.meteor-project.html,meteor-project.css,meteor-project.js安装Iron.Router之后,运行项目会发现,页面展示错误提示内容:

Organize your Meteor application.
Router.route('/', function () {
  this.render('Home', {
    data: function () { return Items.findOne({_id: this.params._id}); }
  });
});
        

我们默认生成的文件运行之后和Iron.Router规则有冲突,这个提醒的意思是我们还没有设置一个"/"的路由规则,我们需要设置路由规则,但是首先要先将meteor默认生成的文件删除。

使用Iron.Router

添加head.html

Iron.router需要设置一个head.html

<head>
    <meta charset="UTF-8">
    <title>Iron-router</title>
</head>

查看网页源代码可以看到head里面多了个title:
图片描述

设置"/"页面

新建一个home.html页面

<template name="home">
    <h1>I am home</h1>
</template>

router.js

新建一个router.js文件。添加一个路由规则:

Router.route('/', function () {
    this.render('home');
});

这段代码的意思是在主目录('/')下,我们指定访问名字为home的template。
现在页面就可以正常的现在home.html的内容了。

简化写法

新建一个content.html。

<template name="content">
    <h2>I am content</h2>
</template>

增加一条路由规则:

Router.route('/content');

这个规则的会自动的指定名字叫做content的template或者页面,这样简单的写法,让你不用再实现后面function的内容,简化了代码,约定大于配置

REST

大多数据的网站都需要支持REST风格的API,Iron.Router不仅仅能够对Meteor的内容做路由,还能增加REST风格的Api。
添加一条router规则:

Router.route('/content/:_id', function () {
    var req = this.request;
    var res = this.response;
    res.end('this is string');
}, {where: 'server'});

where:'server'用来告诉Rotuer这个是服务器端的路由规则.
使用curl看下结果:

curl 'http://localhost:3000/content/1'

返回的结果为:

this is string

并没有返回html和js文件。

项目地址:https://github.com/jjz/meteor/tree/master/meteor-router


http://www.niftyadmin.cn/n/3863245.html

相关文章

[中英双语] 数学缩写列表 (List of mathematical abbreviations)

List of mathematical abbreviations From Wikipedia, the free encyclopedia 数学缩写列表 维基百科&#xff0c;自由的百科全书 This article is a listing of abbreviated names of mathematical functions, function-like operators and other mathematical terminology. 这…

PowerBI开发 第七篇:数据集和数据刷新

PowerBI报表是基于数据分析的引擎&#xff0c;数据真正的来源&#xff08;Data Source&#xff09;是数据库&#xff0c;文件等数据存储媒介&#xff0c;PowerBI支持的数据源类型多种多样。PowerBI Service&#xff08;云端&#xff09;有时不直接访问Data Source&#xff0c;而…

spring的aspectj简介

准备工作 导入对应的包 book是被增强&#xff0c;mybook是增强

假设满足怎样的条件,就不去编程

我甚少写一些纯技术类文章&#xff0c;因为不想重造轮子&#xff0c;而且别人写得比我好一万倍&#xff0c;除非我有的别人没有。以前我会认为“技术总结”可以刻骨铭心&#xff0c;但现在我觉得“生活思考”更扣人心弦。有时候做事情之前建议多问几个为什么&#xff0c;答案不…

UIlabel 的自适应文字

2019独角兽企业重金招聘Python工程师标准>>> [为什么要自适应高度 ? 简单的说, 像微信的聊天框, 气泡是不知道每次输入的内容的, 如此一来, 需要Label适应文字的的高度, 以达到灵活的目的。接下来, 我们来实现这个过程 实现的关键方法介绍 : NSString下面有一个方法…

Android Support兼容包详解Material Design 详解

2019独角兽企业重金招聘Python工程师标准>>> Android Support兼容包详解&#xff1a;http://www.open-open.com/lib/view/open1427852683115.html Android Support 包里究竟有什么&#xff1a;http://www.2cto.com/kf/201411/350928.html Android Material Design 详…

[Android]Dagger2Metrics - 测量DI图表初始化的性能(翻译)

以下内容为原创&#xff0c;欢迎转载&#xff0c;转载请注明 来自天天博客&#xff1a;http://www.cnblogs.com/tiantianbyconan/p/5098943.html Dagger2Metrics - 测量DI图表初始化的性能 原文&#xff1a;http://frogermcs.github.io/dagger2metrics-measure-performance-of-…

SSL/TLS协议簇加解密流程

一、概述 SSL协议是最早Netscape公司开发的安全通信协议&#xff0c;用于浏览器安全通信。到SSL Version3&#xff0c;提交作为IFTF草案&#xff0c;已经广泛的应用Intetnet通信。之后IETF对SSLv3稍作改动并更名为TLS1.0&#xff0c;对应RFC2246&#xff0c;之后的TLS1.1、TLS1…