Content middleware you can add to your Connect/Express app.
Switch branches/tags
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Failed to load latest commit information.



var connectContent = require('connect-content');

var content = connectContent({
    contentDir : path.join('/', __dirname, 'content'),

// later on
app.get('/', content);
app.get('/:pagename', content);

This example will serve this content at the root level. You must set both / and /:pagename so that we can show the 'index' page at / since Express doesn't call us for / when using only /:pagename. The /:pagename parameter must also be called pagename, not anything else, otherwise connect-content wouldn't know where to look.

What is connect-content

'connect-content' is middleware for Express/Connect. It can read a directory full of static md|txt|html|json|ini files and serve up some content for each.

Note that md, txt and html files are content files. And json and ini files are metadata files. You should only have one of each type for each path. e.g.

If, for example you had a directory of files:

This would serve up the following files (if connect-content was mounted at /)::

* /
* /about
* /code

Once 'connect-content' has read those files in, it will create a structure similar to the following:

    name    : 'my-page',
    content : ' ... the entire content from the *.md, *..html or *.txt file ... ',
    html    : ' ... the HTML from the HTML conversion ... ',
    meta    : {
        // then entire data read from the *.json or *.ini file

Default Options

The default options have reasonable defaults for each of these keys, but you can provide your own if you like:

var defaults = {
    title       : 'Content',
    contentDir  : 'content',
    template    : 'content-page',
    localsVar   : 'content',

var opts = {
    title       : 'My Projects',
    contentDir  : path.join('/', __dirname, 'projects'),
    template    : 'project-page',
    localsVar   : 'projects',

The Template

The default template used is content-page so a really simple example (in Pug) of that is:

extends layout

block content
  h1= content.title
  | !{}

This means that the title is placed into the h1 and the html that has been generated is placed (unescaped) underneath that. Since we are using Markdown or Textile for the page content, any HTML escaping has already occurred.


Generally there are two types of file in your content directory. Content files and meta files.

  • content ** *.html ** *.md ** *.textile ** *.txt
  • meta ** *.json ** *.ini ** *.yaml

Therefore, for each page you want one of each type. For an about page you could have:

  • about.html
  • about.json

If you also have an, then only one of about.html and will be shown (and it is not specified which one) so make sure you only have one of each type in the contentDir.


Andrew Chilton: