TypeError:string不是函数?

时间:2015-08-09 08:11:33

标签: javascript node.js mongodb

你好,我是节点js的新手

我正在尝试运行以下代码

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');

然后出现错误

  

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

2 个答案:

答案 0 :(得分:0)

只需查看express res.render()的文档即可。我认为你的语法有问题。

// 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) {
  // ...
});

答案 1 :(得分:0)

代码没有任何问题,对我来说工作正常

执行以下操作来调试问题

  1. 检查hello.html文件夹中的views文件,删除所有文件 HTML代码,并试着把一些简单的" Hello Word"
  2. 创建新文件夹并复制粘贴js脚本和视图 文件夹然后尝试npm install express,巩固和swig
  3. 检查v5.5.0的节点版本,它没有任何正常工作 问题