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)
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');
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With