Total Pageviews

Sunday, 23 February 2014

在linux vps上搭建基于nodejs的静态博客程序-jen

git clone https://github.com/rfunduk/jen
cd jen
ln -s ~/jen/bin/jen /usr/local/bin/jen
npm install
cd skeleton
ls
显示:
config.coffee _layouts  _posts _scripts  _styles
_pages README.md  _statics
nano config.coffee
(把config.coffee文件里的PORT的值改为其他端口号,比如3035,因我服务器的3000端口已被另一个程序占用了)
nohup jen > /dev/null &
(访问http://as3.brite.biz:3035/就可看到网站效果)
jen --build (这个就是生成/更新静态网站的命令)

as3:~/jen/skeleton# ls
build          _layouts  _posts     _scripts  _styles
config.coffee  _pages    README.md  _statics
(新出现了build目录)
as3:~/jen/skeleton# cd build
as3:~/jen/skeleton/build# ls
css          index.html  more-about-deployment      
favicon.ico  intro       more-about-layout
home         js          more-about-posts-and-pages
as3:~/jen/skeleton/build# nohup Rwebserver 34273 > /dev/null &
可见~/jen/skeleton/build/就是静态网站的根目录。
访问http://as3.brite.biz:34273/就可看到网站效果.

发贴方法:
as3:~/jen/skeleton/build# cd ..
as3:~/jen/skeleton# ls
build          _layouts  _posts     _scripts  _styles
config.coffee  _pages    README.md  _statics
as3:~/jen/skeleton# cd _posts
as3:~/jen/skeleton/_posts# ls
misc-stuff.jade           more-about-layout.md          
more-about-deployment.md  more-about-posts-and-pages.md
as3:~/jen/skeleton/_posts# nano test1.md
格式为:
title: "test1"
date: "23.02.2014"
layout: "test"
---

这是测试1.


然后,
as3:~/jen/skeleton/_posts# cd ..
as3:~/jen/skeleton# jen --build

演示站点:http://jen.briteming.yi.org
http://as3.brite.biz:3035/,http://as3.brite.biz:34273/
http://as3.brite.biz:3035/intro
http://as3.brite.biz:3035/more-about-layout
http://as3.brite.biz:3035/more-about-posts-and-pages
http://as3.brite.biz:3035/more-about-deployment
http://as3.brite.biz:34273/test1/
http://as3.brite.biz:34273/test2/
http://as3.brite.biz:34273/smiling-face/
http://as3.brite.biz:34273/chinese-economy/
项目地址:https://github.com/rfunduk/jen

只是已发表的帖子并不自动出现在首页,可这样操作:
as3:~/jen/skeleton# nano _pages/home.md
 在Welcome to the `jen` skeleton site此行的上方加上:
<p><a href=/chinese-economy/>中国经济</a></p>
<p><a href=/smiling-face/>笑脸</a></p>
<p><a href=/test2/>测试2</a></p>
<p><a href=/test1/>测试1</a></p>

然后,
as3:~/jen/skeleton# jen --build



问问作者如何让帖子自动出现在首页.
程序作者的答复:
Nothing happens automatically in jen. You just have access to various variables and stuff in the scope of a page or post, and you build your own pages. If you have a page called ‘home’ and you set it up as the root of the site (via the ‘index’ key in config.coffee), you can access all pages and posts via ‘pages’ and ‘posts’ variables in that template.

For example here’s what I used for my old ‘archive’ page:

title: "Archives"
sidebar: true
skip_posts: true
description: "The archives of posts at ryanfunduk.com"
---
<%
  posts_by_year = _.reduce( posts, function( pby, post ) {
    var key = post.timestamp.native().getFullYear();
    if( !pby.hasOwnProperty(key) ) { pby[key] = []; }
    pby[key].push( post );
    return pby;
  }, {} );
%>

<div id="archives">
  <% _.keys( posts_by_year ).sort().reverse().forEach( function( year ) { %>
    <div class="year" id="archiver-<%= year %>">
      <h2><%= year %></h2>
      <ul class="posts">
        <% posts_by_year[year].forEach( function( post, i ) { %>
          <li><a href="<%= '/' + post.permalink %>"><%= post.title %></a></li>
        <% } ); %>
      </ul>
    </div>
  <% } ) %>
</div>

So you see you just manipulate and use the data exposed to the template as you like. I also put a list of featured posts in my config.coffee:

post_count: 6
featured_posts: [
  'shared-auth-for-rack-apps'
  'lmklater'
  'talking-about-dvcs'
  'validation-considered-harmful'
]

Then in my layout I made a footer that shows the posts:

.list#posts
  h3 featured posts
  ul
    - onSidebar = posts.slice( 0, config.post_count );
    - posts.forEach( function( post, i ) {
      - if( config.featured_posts.indexOf(post.permalink) == -1 ) { return; }
      li
        a( href="/" + post.permalink )= post.title
    - } );

If you’re still really early in your development of the site I really suggest trying something else. Like I was saying earlier, jen is a really rough hackjob of a project that I did to learn/experiment with node. Nanoc or Jekyll would be much better, more stable and better supported tools to do this.

The biggest problems with jen that I’m aware of are the previously mentioned incompatibility with recent node versions (at least I was pretty sure it didn’t work), and more importantly, it opens way too many files during compilation which often results in bumping into the file descriptor limit on most operating systems (this breaks jen completely and you can’t continue to use it without increasing the limit permanently or reducing the number of files in the project — kind of defeats the point of blog software dont you think! :)).