Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NodeJs Using ExpressJs : TypeError: string is not a function at Function.app.render

I just started learning Node and I am trying to build a web application using Node and Express. And I have the following code in my app.js file, with the following directory structure.

Directory Structure:

app
   assets
   controller
   model
   view
      index.jade
global
node_modules
app.js
package.json

-js-

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

app.configure(function() {
  app.set('view', __dirname + '/app/view');
  app.set('view engine', 'jade');
  app.use(app.router);
});

app.get('/', function(req, res){
    res.render('index', {title: 'express'});
});

app.listen(3000);
console.log('Listening on port 3000');

After running the command node app and going to localhost:3000. I get the following error. I am assuming it doesn't like the string on this line -> res.render('index', {title: 'express'});. However from everything I have found on Google this seems right. So I must be missing something else.

ERROR MESSAGE:

TypeError: string is not a function at Function.app.render (C:\myapp\express\node_modules\express\lib\application.js:488:12) at ServerResponse.res.render (C:\myapp\express\node_modules\express\lib\response.js:803:7) at C:\myapp\express\app.js:19:6 at callbacks (C:\myapp\express\node_modules\express\lib\router\index.js:164:37) at param (C:\myapp\express\node_modules\express\lib\router\index.js:138:11) at pass (C:\myapp\express\node_modules\express\lib\router\index.js:145:5) at Router._dispatch (C:\myapp\express\node_modules\express\lib\router\index.js:173:5) at Object.router (C:\myapp\express\node_modules\express\lib\router\index.js:33:10) at next (C:\myapp\express\node_modules\express\node_modules\connect\lib\proto.js:190:15) at Object.expressInit [as handle] (C:\myapp\express\node_modules\express\lib\middleware.js:30:5)

like image 489
Kris Hollenbeck Avatar asked Oct 21 '13 17:10

Kris Hollenbeck


1 Answers

I think this is just a typo/mistake setting 'view' (singular) instead of 'views' (plural). Check out this example. I think the express application object has both 'view' and 'views' settings but they mean different things.

https://github.com/visionmedia/express/blob/master/examples/jade/index.js

Here's your fix to be clear:

  app.set('views', __dirname + '/app/view');
like image 73
Peter Lyons Avatar answered Sep 17 '22 22:09

Peter Lyons