Feathers.js – 加载静态内容

我正在评估一个项目的羽毛.js 。 我喜欢它的愿望。 所以,我决定尝试build立一个基本的内容pipe理系统,就像学习一样。 事情进展得相当顺利。 不过,我想在应用程序启动时将一些静态内容(文章)加载到内存中。 我无法弄清楚如何做到这一点。

我在data / articles目录中有我的文章。 每篇文章都是名为[title] .md的降价。 我有一个JavaScript块,我在一个控制台应用程序testing,将降价转换为HTML。 该代码使用markdown-js将HTML获取到JSON对象中。 它看起来像这样:

const fs = require('fs'); const markdownConverter = require('markdown'); let articles = []; let files = fs.readdirSync('./data/articles'); for (let i=0; i<files.length; i++) { let title = files[i].substr((files[i].lastIndexOf('/')+1), (files[i].length-3)); let markdown = fs.readFileSync(files[i], 'utf8'); let html = markdownConverter.toHTML(markdown); articles[title] = html; } 

我已经在羽毛中添加了这样的路线:

 app.use('/articles/:slug', function(req, res) { console.log('loading article: ' + req.params.slug); let content = ''; // TODO: How to get access to the articles array. // I want to get the HTML using content = articles[req.params.slug]; res.render('article', { content: content }); }); 

我不知道在哪里把加载markdown的代码放到我可以在用户请求文章时访问的数组中。 那属于哪里? 我的猜测是在使用yeoman生成器创build羽毛项目时生成的app.js文件。 然而,我不确定那实际上是什么样子。

由于羽毛是一个快速的应用程序,你应该能够使用快递中间件。 我推荐这个可以让你为markdown创buildHTML模板并静态地提供它们,而不需要创build任何parsing器或者循环。

https://github.com/natesilva/node-docserver

 var docserver = require('docserver'); ... app.use(docserver({ dir: __dirname + '/docs', // serve Markdown files in the docs directory... url: '/'} // ...and serve them at the root of the site )); 

或者,这个中间件将在Jade模板中将其作为HTML服务之前预压缩Markdown。

https://www.npmjs.com/package/markdown-serve