-1

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?

Ash
  • 5,057
  • 7
  • 35
  • 49
  • possible duplicate of [How to specify HTTP error code in express.js?](http://stackoverflow.com/questions/10563644/how-to-specify-http-error-code-in-express-js) – OrangeDog Sep 17 '14 at 13:26
  • @OrangeDog, Not a duplicate, that is about attempting to set the http status code, this is about node/express crashing when attempting to render a view. – Ash Sep 17 '14 at 13:51
  • Either you're setting it wrongly, your installation is broken, or there's something you've not told us. – OrangeDog Sep 17 '14 at 13:53
  • I wish I knew what else to tell, the node/express installation seems fine because I am serving a whole restful api with it, just got a new requirement through for html pages to be rendered. – Ash Sep 17 '14 at 14:03
  • A minimal working example that demonstrates the problem. – OrangeDog Sep 17 '14 at 14:04

2 Answers2

0

app.set('views', __dirname + './views'); should probably be app.set('views', __dirname + '/views');

mscdex
  • 104,356
  • 15
  • 192
  • 153
0

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.

Ash
  • 5,057
  • 7
  • 35
  • 49