express jade

    技术2022-07-11  124

    express jade

    RSS feeds are a great way to facilitate a loyal readership. In fact, as I write this, the RSS feed in David's sidebar is touting over 11,400 subscribers. Hitting the front page of Hacker News is always nice, but for most sites that's not going to translate into a reliable source of traffic. Getting each and every post in front of thousands of intentional subscribers though (who have their own followers on Twitter, Google+, etc.)? That's a traffic generator.

    RSS feed是促进忠实读者的好方法。 实际上,在我撰写本文时,David侧边栏中的RSS feed吸引了11400个订阅者。 进入Hacker News的首页总是很不错的,但是对于大多数网站而言,这并不会转化为可靠的流量来源。 但是,让每条帖子都在成千上万的有意订阅者面前(谁在Twitter,Google +等上拥有自己的关注者)? 那是流量生成器。

    There's really only one gotcha to RSS feeds - you actually have to have one. A little over a month ago I launched a new blog, DevSmash. My shiny new site had been public for less than 48 hours when I received a tweet asking where my RSS feed was. Not if I had an RSS feed, but where was my RSS feed. You see, I had the good fortune of receiving some decent promotion on one of my first posts. Users started showing up, and evidently some of them looked for some means of subscribing. I have an RSS feed now, of course, but all those users who showed up in that first week or so - they're long gone. Moral of the story: RSS feeds are great, but you need it before your readers show up.

    RSS提要实际上只有一个陷阱-实际上,您必须拥有一个。 一个多月前,我启动了一个新博客DevSmash 。 当我收到一条推文询问我的RSS提要在哪里时,我闪亮的新网站已经公开发布了不到48小时。 如果我有RSS供稿, 则不是,但是RSS供稿在哪里 。 您知道,我很幸运在我的第一篇文章中获得了不错的晋升。 用户开始出现,显然其中一些人正在寻找订阅的方式。 当然,我现在有一个RSS feed,但是在第一周左右出现的所有用户-他们早已不在了。 故事的寓意:RSS提要很棒,但是在读者出现之前您需要它。

    Alright - let's call that sufficient context. DevSmash is built on top of all the newfangled goodness you could ask for: Node.js, Express, Jade, Stylus, MongoDB, Mongoose, etc. It's a stack that I absolutely love hacking on, but it exposes an intentionally lean feature set, so "rolling your own xyz" often comes with the territory. Such was the case with my RSS feed. This post provides an overview of how I built the DevSmash RSS feed, and I hope that it will be useful to others building on this increasingly popular stack.

    好吧-我们称之为足够的上下文。 DevSmash建立在您可能会要求的所有新奇优点之上:Node.js,Express,Jade,Stylus,MongoDB,Mongoose等。它是我绝对喜欢黑客攻击的堆栈,但是它公开了精益的精益功能集,因此,“滚动自己的xyz ”通常伴随着这一领域。 我的RSS feed就是这种情况。 这篇文章概述了我如何构建DevSmash RSS feed,并且希望它对在此日益流行的堆栈上进行构建的其他人有用。

    角色转换 (Cast of Characters)

    Before we get started, let's do a quick overview of the main technologies we'll be using:

    在开始之前,让我们快速概述一下将要使用的主要技术:

    表达 (Express)

    From the Express home page: "Express is a minimal and flexible node.js web application framework, providing a robust set of features for building single and multi-page, and hybrid web applications." What TJ Holowaychuk is too modest to say here, is that Express has become the de facto standard for building web apps on Node. There are of course other options, but you definitely owe it to yourself to check it out if you haven't already.

    在Express主页上 :“ Express是一个最小且灵活的node.js Web应用程序框架,为构建单页和多页以及混合Web应用程序提供了一组强大的功能。” TJ Holowaychuk在这里过于谦虚地说,Express已成为在Node上构建Web应用程序的事实上的标准。 当然还有其他选择,但是如果您还没有的话,可以肯定要自己检查一下。

    Website: http://expressjs.com/

    网站: http : //expressjs.com/

    (Jade)

    From the Jade readme: "Jade is a high performance template engine heavily influenced by Haml and implemented with JavaScript for node." This is my go-to template engine of choice - terse, feature rich, and a syntax that reads as well as it writes.

    来自Jade自述文件 :“ Jade是一种高性能模板引擎,受Haml的影响很大,并使用JavaScript实现了节点。” 这是我首选的模板引擎-简洁,功能丰富且语法可读写。

    Website: http://jade-lang.com/

    网站: http : //jade-lang.com/

    猫鼬 (Mongoose)

    From the Mongoose GitHub repo: "Mongoose is a MongoDB object modeling tool designed to work in an asynchronous environment." In other words, Mongoose provides a model layer for interacting with your MongoDB collections from Node.

    来自Mongoose GitHub存储库 :“ Mongoose是旨在在异步环境中工作的MongoDB对象建模工具。” 换句话说,Mongoose提供了一个模型层,用于与Node中的MongoDB集合进行交互。

    Website: http://mongoosejs.com/

    网站: http : //mongoosejs.com/

    Note: It's relatively inconsequential that we're using Mongoose in this post. The concepts should translate well enough to however you're managing your persistence.

    注意:本文中使用猫鼬相对无关紧要。 这些概念应该可以很好地转换为您要管理的持久性。

    RSS要求 (RSS Requirements)

    One last thing before we dig into the code - let's identify our basic requirements for our RSS feed:

    在深入研究代码之前的最后一件事-让我们确定对RSS feed的基本要求:

    The feed should include the most recent 20 published posts.

    该供稿应包含最近发布的 20条帖子。

    Output should be a valid RSS 2.0 feed (I'm personally using the W3C feed validator to verify).

    输出应为有效的RSS 2.0提要(我个人使用W3C提要验证器进行验证)。

    Simple enough.

    很简单。

    代码 (The Code)

    For the sake of this article, we only need to be concerned with three files:

    为了本文的目的,我们只需要关心三个文件:

    blog-post.js: Our BlogPost Mongoose Model (implementation details aren't too important for this article, but it's included for the sake of completeness).

    blog-post.js :我们的BlogPost猫鼬模型(实现细节对于本文而言不太重要,但出于完整性考虑,将其包括在内)。

    feed.js: Our route handler (responsible for fetching posts from the database and feeding them to our view).

    feed.js :我们的路线处理程序(负责从数据库中获取帖子并将其提供给我们的视图)。

    rss.jade: Our RSS template (responsible for turning our posts into a valid RSS 2.0 feed).

    rss.jade :我们的RSS模板(负责将我们的帖子变成有效的RSS 2.0 feed)。

    blog-post.js (blog-post.js)

    We won't spend too much time talking about this file - it's purely here for reference since we'll be interacting with it later on.

    我们不会花太多时间谈论这个文件-纯粹在这里供参考,因为我们稍后将与它进行交互。

    var mongoose = require('mongoose'), Schema = mongoose.Schema; var BlogPostSchema = new Schema({ title: { type: String, required: true, trim: true }, slug: { type: String, required: true, lowercase: true, trim: true, index: { unique: true } }, body: { type: String, required: true }, teaser: { type: String, required: true }, author: { type: String, required: true, trim: true }, published: { type: Boolean, required: true, default: false }, createdAt: { type: Number }, updatedAt: { type: Number } }); // update timestamps on save BlogPostSchema.pre('save', function(next) { this.updatedAt = Date.now(); if (this.isNew) this.createdAt = this.updatedAt; next(); }); // create and export our model module.exports = mongoose.model('BlogPost', BlogPostSchema);

    feed.js (feed.js)

    A common Express convention for route handlers is to put them into a dedicated routes/ folder. In my own apps I generally have my route files export a single function that accepts the Express application instance, like so:

    路由处理程序的常用Express约定是将它们放入专用的routes/文件夹中。 在我自己的应用程序中,通常我的路由文件导出一个接受Express应用程序实例的函数,如下所示:

    // some-route-handler.js module.exports = function(app) { app.get('/some/path', function(req, res, next) { // handler logic }); };

    With a code structure like this in place, your main app.js file simply needs a line like the following:

    有了这样的代码结构,您的主app.js文件仅需要一行,如下所示:

    require('./routes/some-route-handler')(app);

    Alright then, here's what a functional RSS handler actually looks like:

    好吧,这就是功能RSS处理程序的实际外观:

    var BlogPost = require('../lib/model/blog-post'); module.exports = function(app) { app.get('/feed/rss', function(req, res) { BlogPost.find({}) .sort('-publishedAt') .where('published', true) .limit(20) .select('title slug publishedAt teaser') .exec(function(err, posts) { if (err) return next(err); return res.render('rss' { posts: posts }); }); }); };

    As can be seen, there's not much need for cruft around populating our RSS feed. We simply query for the most recent 20 published posts, and render them with our RSS template. Which brings us to...

    可以看出,在填充RSS提要时并不需要太多东西。 我们只需查询最近发布的20条帖子,然后使用我们的RSS模板进行渲染即可。 这带我们去...

    rss.jade (rss.jade)

    While Jade's primary use case is generating HTML output, it is just as handy at generating XML. Here's what our Jade template looks like:

    虽然Jade的主要用例是生成HTML输出,但在生成XML方面也非常方便。 这是我们的Jade模板的样子:

    doctype xml rss(version='2.0', xmlns:atom='<a href="http://www.w3.org/2005/Atom" rel="nofollow">http://www.w3.org/2005/Atom</a>') channel title DevSmash link <a href="http://devsmash.com" rel="nofollow">http://devsmash.com</a> atom:link(href='<a href="http://devsmash.com/feed/rss" rel="nofollow">http://devsmash.com/feed/rss</a>', rel='self', type='application/rss+xml') description Developers talking about stuff that developers like to talk about. language en-US if posts.length lastBuildDate= new Date(posts[0].publishedAt).toUTCString() each post in posts item title= post.title link <a href="http://devsmash.com/blog/#{post.slug}" rel="nofollow">http://devsmash.com/blog/#{post.slug}</a> description | <![CDATA[ | !{post.teaser} p: a(href='<a href="http://devsmash.com/blog/#{post.slug}')!=" rel="nofollow">http://devsmash.com/blog/#{post.slug}')!=</a> 'Read more »' | ]]> pubDate= new Date(post.publishedAt).toUTCString() guid(isPermaLink='false') <a href="http://devsmash.com/blog/#{post.slug}" rel="nofollow">http://devsmash.com/blog/#{post.slug}</a>

    The Jade syntax may look a little foreign if this is your first time seeing it, but for the most part things are fairly self-explanatory. There are a few things worth pointing out though:

    如果您是第一次看到Jade语法,可能看起来有些陌生,但是在大多数情况下,它们是不言而喻的。 但是,有些事情值得指出:

    The atom stuff isn't strictly required, but was suggested by the W3C feed validator.

    并不是绝对需要atom东西,但是W3C feed验证程序建议了atom东西。

    Outputting the post body (or teaser in this case) requires some special care. You can't encode the markup, or you'll simply see encoded HTML in your RSS reader, but at the same time we need to protect the XML. The standard solution, then, is to wrap the post markup inside of CDATA tags.

    输出桩身(在这种情况下为预告片)需要特别注意。 您无法对标记进行编码,或者只是在RSS阅读器中看到已编码HTML,但与此同时,我们需要保护XML。 然后,标准解决方案是将发布标记包装在CDATA标记内。

    And there you have it! Not even 40 lines of code (not counting the model) for a custom RSS feed. I hope this was helpful, and I'd love to hear any thoughts, questions, or concerns in the comments!

    在那里,您拥有了! 自定义RSS Feed甚至没有40行代码(不计算模型)。 我希望这会有所帮助,并且很乐意在评论中听到任何想法,问题或疑虑!

    翻译自: https://davidwalsh.name/rss-express

    express jade

    相关资源:25个经典网站源代码
    Processed: 0.032, SQL: 9