Bye Bye Moore

PoCソルジャーな零細事業主が作業メモを残すブログ

express4でjadeをつかう

あまりに久々すぎて完全にやり方を忘れてたのでメモ

実際のところ

単純に、トップページにjadeをレンダリングした内容を表示したいだけなら、以下の通りです。

app.js

var express = require('express');
var app     = express();

app.use('/data', express.static('data'));
app.set('view engine', 'jade');

app.get('/', function(req, res) {
  res.render('index', { title: 'Hey', message: 'Hello there!'});
});

app.listen(3000, function () {
  console.log('Example app listening on port 3000!');
});

viewsディレクトリには以下のようなindex.jadeファイルをおきます。
ディレクトリ名をviewとか単数形にすると認知しないので気をつけて下さい(白目

html
  head
    title= title
  body
    h1= message

最終的なディレクトリ構造は以下のようになります

$ tree -L 2
.
├── app.js
├── data
│   └── Flag_of_Japan.png
├── node_modules
│   ├── express
│   ├── jade
│   └── public
├── package.json
└── views
    └── index.jade