1
votes

hello guys I am new to node js

i am trying to run following code

var express = require('express'),
    app = express(),
    cons = require('consolidate'); // Templating library adapter for Express

    app.engine('html', cons.swig);
    app.set('view engine', 'html');
    app.set('views', __dirname + '/views');

    app.get('/', function(req, res){
        res.render('hello', { name : 'World' });
    });

    app.get('*', function(req, res){
        res.send('Page Not Found', 404);
    });

    app.listen(8080);
    console.log('Express server started on port 8080');

then error comes

TypeError: string is not a function at EventEmitter.render (C:\Users\YAm\Desktop\mongodb\work\node_modules\express\lib\application.js:569:12) at ServerResponse.render (C:\Users\YAm\Desktop\mongodb\work\node_modules\express\lib\response.js:961:7) at C:\Users\YAm\Desktop\mongodb\work\test_coso.js:10:6 at Layer.handle [as handle_request] (C:\Users\YAm\Desktop\mongodb\work\node_modules\express\lib\router\layer.js:95:5) at next (C:\Users\YAm\Desktop\mongodb\work\node_modules\express\lib\router\route.js:131:13) at Route.dispatch (C:\Users\YAm\Desktop\mongodb\work\node_modules\express\lib\router\route.js:112:3) at Layer.handle [as handle_request] (C:\Users\YAm\Desktop\mongodb\work\node_modules\express\lib\router\layer.js:95:5) at C:\Users\YAm\Desktop\mongodb\work\node_modules\express\lib\router\index.js:277:22 at Function.process_params (C:\Users\YAm\Desktop\mongodb\work\node_modules\express\lib\router\index.js:330:12) at next (C:\Users\YAm\Desktop\mongodb\work\node_modules\express\lib\router\index.js:271:10) at expressInit (C:\Users\YAm\Desktop\mongodb\work\node_modules\express\lib\middleware\init.js:33:5) at Layer.handle [as handle_request] (C:\Users\YAm\Desktop\mongodb\work\node_modules\express\lib\router\layer.js:95:5) at trim_prefix (C:\Users\YAm\Desktop\mongodb\work\node_modules\express\lib\router\index.js:312:13) at C:\Users\YAm\Desktop\mongodb\work\node_modules\express\lib\router\index.js:280:7 at Function.process_params (C:\Users\YAm\Desktop\mongodb\work\node_modules\express\lib\router\index.js:330:12) at next (C:\Users\YAm\Desktop\mongodb\work\node_modules\express\lib\router\index.js:271:10)

2
have you installed all the dependencies like expressjs and consolidate?Rudra
It looks like your call to res.render() is being served by the Express implementation which expects res.render(view [, locals] [, callback]). My guess it that either your templating/view engine is not properly installed or initialized or you are not using it correctly.jfriend00

2 Answers

0
votes

Just check out the documentation for express res.render(). I think there is a problem with your syntax.

// send the rendered view to the client
res.render('index');

// if a callback is specified, the rendered HTML string has to be sent explicitly
res.render('index', function(err, html) {
  res.send(html);
});

// pass a local variable to the view
res.render('user', { name: 'Tobi' }, function(err, html) {
  // ...
});
0
votes

The code is not having any problem and is working fine for me

Do the following things to debug the issue

  1. Check your hello.html file inside views folder remove all the html code and try to put some simple "Hello Word"
  2. Create a new Folder and copy paste the js script and the views folder then try npm install express, consolidate and swig
  3. Check the node version for v5.5.0 it is working fine without any issue