Node.js + express + jade での静的ルーティング
(追 ルーティングって言ったら普通動的ルーティングです( /user/:id 的な)静的なほうを話そうとするのは僕くらいなもんです
Node.js + express + jade での静的ページのルーティング
ためしに連想配列でページを追加できるよう書いてみたんです。
app.coffee
express = require("express") routes = require('./routes') # . . routes.init(app) #
routes.coffee #
siteTitle = 'piglovesyou' pageTable = '/': 'ホームページ' # render views/index.jade '/about': 'このサイトについて' # render views/about.jade '/jquery': # (directory) '/': 'jQueryプラグイン一覧' # render views/jquery/index.jade '/flickgal': 'flickGal-jQueryプラグイン' # render views/jquery/flickgal.jade # @type {Object} # @type {Function} takes 3 args: iter = (obj, fn, path_ = '') -> for key of obj path = if path_ then path_ + key else key dir = if key is '/' then path + 'index' else path if typeof obj[key] is 'string' fn obj[key], path, dir.replace(/^\//, '') else iter obj[key], fn, path exports.init = (app) -> iter pageTable, (title, path, dir) => app.get path, (req, res) -> res.render(dir, { title: title siteTitle: siteTitle })
expressとかですでにAPI提供されてたりして・・
-
- -
追記
あぁ http://expressjs.com/guide.html#routing
-
- -
追記
やっぱり、ちょうどいいのが見つかりません やろうとしていることをNode でやる必要がないのは分かっているんですが。
プラグインにもない。というかそういうものはRailsにも無かった。やろうとしていることは単純に間違っているか時流遅れ。
- -
追記
最終的にこうしました。
_ = require('underscore') SITE_TITLE = 'stakamura' pageTable = '/': 'ホームページ' '/about': 'このサイトについて' '/jquery': '/': 'jQueryプラグイン一覧' '/flickgal': '/': 'flickGal - jQueryプラグイン' '/demo': 'flickGal - デモ' '/putitscroll': '/': 'putitScrill - jQueryプラグイン' '/demo': 'putitScrill - デモ' '/playground': '/': 'playground(遊び場)' '/111013': {'/': '15パズルcoffee'} '/110223': {'/': 'twitterあそび'} '/110124': {'/': '15パズル2(ツー)'} '/110111': {'/': '15パズル?'} # @param {Object} # @param {Function} takes 3 args: title, path, jadeDir iter = (table, fn, path_ = '') -> for key of table path = if path_ then path_ + key else key dir = if key is '/' then path + 'index' else path if typeof table[key] is 'string' fn( title: table[key] path: path, jadeDir: dir.replace(/^\//, '') ) else iter table[key], fn, path # @param {String} path # @return {Array.<Object>} createBreadcrumb = (path) -> result = [{path: '/', title: pageTable['/']}] currentPath = '' currentObj = pageTable path.replace /\/[^\/]*/g, (fragment) -> target = currentObj[fragment] if _.isObject target title = target['/'] currentObj = target else return if fragment == '/' title = target currentPath += fragment result.push path: if fragment != '/' then currentPath else currentPath + 'index' title: title result exports.init = (app) -> iter pageTable, (page) => breadcrumbs = createBreadcrumb(page.path) app.get page.path, (req, res) -> res.render page.jadeDir, breadcrumbs: breadcrumbs pagePath: page.path siteTitle: SITE_TITLE pageTitle: page.title