I've configured my nodejs/express application as follows;
app.set('views', __dirname + './views');
app.set('view engine', 'ejs');
Then I added a route like so
app.get('/page/MyPage', function(req, res) {
// res.statusCode = 200; // Setting the status code here has no effect on the error.
res.render('MyPage', { Data: "Stuff" });
});
However, whenever I request the page http://localhost:8000/page/MyPage node crashes out with TypeError: Cannot call method 'toString' of undefined, breaking in the http.js class.
The exact line:
var statusLine = 'HTTP/1.1 ' + statusCode.toString() + ' ' +
reasonPhrase + CRLF; // Aprox line 1180, in ServerResponse.prototype.writeHead = function(statusCode) function.
I've tried adding extensions to the 'MyPage' within render, and also tried other view engines. All of which yield the same result.
The view does exist in the /views directory.
Does anybody have any suggestions?
app.set('views', __dirname + './views'); should probably be app.set('views', __dirname + '/views');
Solution:
After stepping through an awful lot of javascript I have identified the issue. It appears that although the express documentation (http://expressjs.com/guide.html) suggests this is done automatically, I needed to set the view engine manually.
In this instance I also am using the vash view engine.
app.set('views', __dirname + '/views');
app.set('view engine', 'vash');
app.engine('.vash', require('vash').renderFile);
This allows my views to be rendered as expected.