3
votes

This code:

var app = require('express').createServer();
var ejs = require('ejs');
var fs = require('fs');
var indexTemplate = fs.readFileSync(__dirname + '/templates/index.html');

app.get('/', function(req, res){
    res.end(ejs.render(indexTemplate, {}));
});

Gives me the following error

TypeError: Object Address has no method 'substr' at Object.parse (/home/lowerkey/node_modules/ejs/lib/ejs.js:152:20) at /home/lowerkey/node_modules/ejs/lib/ejs.js:192:13 at Object.render (/home/lowerkey/node_modules/ejs/lib/ejs.js:235:10) at /home/lowerkey/workspace/address book/server/server.js:60:14 at callbacks (/home/lowerkey/node_modules/express/lib/router/index.js:272:11) at param (/home/lowerkey/node_modules/express/lib/router/index.js:246:11) at pass (/home/lowerkey/node_modules/express/lib/router/index.js:253:5) at Router._dispatch (/home/lowerkey/node_modules/express/lib/router/index.js:280:4) at Object.handle (/home/lowerkey/node_modules/express/lib/router/index.js:45:10) at next (/home/lowerkey/node_modules/express/node_modules/connect/lib/http.js:203:15)

What am I doing wrong?

1

1 Answers

6
votes

Your indexTemplate variable actually contains buffer instead of string, due to default encoding of the readFile return value. You may want to try to add the encoding in readFileSync 2nd parameter to set it, like the following:

...
var indexTemplate = fs.readFileSync(__dirname + '/templates/index.html', 'utf8');
...