Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Node.js - EJS example

Tags:

node.js

ejs

I am trying to use Embedded Javascript renderer for node. I installed it using npm, as given here: https://github.com/visionmedia/ejs

And I have the following code, but it does not seem to work:

var connect = require('connect'),
 ejs = require('ejs');

var server = connect.createServer(
    connect.bodyDecoder(),
    connect.methodOverride(),
    connect.staticProvider(__dirname + '/public'),
    function(req,res) {
     ejs.render('hi');
    }
);


server.listen(9000);

Any help greatly appreciated.

like image 839
jeffreyveon Avatar asked Jan 05 '11 04:01

jeffreyveon


1 Answers

try this: (assuming you have the express and ejs modules installed)

var express = require('express');
var app = express.createServer();
app.configure(function() {
    app.use(express.bodyParser());
    app.use(express.static('./static/'));
    app.use(app.router);
});
app.set('view engine', 'ejs');
app.set('view options', {
    layout: false
});
app.get('/', function(req, res) {
res.render('index', {
    message : 'De groeten'
});
});
app.listen(3000);

and put a view in './views'. call it 'index.ejs' and fill it with some html:

<html>
<head>
<title></title>
</head>

<body>
<p>
<%= message %>
</p>
</body>
</html>

works for me!

like image 162
Elmer Avatar answered Sep 19 '22 11:09

Elmer