NodeJS错误:无法找到模块' [object Object]'

时间:2017-09-17 07:34:01

标签: node.js

我有这个简单的NodeJs代码

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


app.disable('x-powered-by');

var handleBars = require('express-handlebars').create({defaultLayout:'main'});

app.engine('handlebars',handleBars.engine);
app.set('view engine',handleBars);

app.set('port',process.env.PORT || 3000);

app.get('/',function(req,res){
    res.render('home');
})

app.listen(app.get('port'),function(){
    console.log('Working boy');
})

和两个文件main.handlebars and home.handlebars。但是当我运行此代码时,它会给我以下错误

Error: Cannot find module '[object Object]'
    at Function.Module._resolveFilename (module.js:469:15)
    at Function.Module._load (module.js:417:25)
    at Module.require (module.js:497:17)
    at require (internal/module.js:20:19)
    at new View (/Users/muradshahmammadli/Desktop/progr/NodeJS/node_modules/express/lib/view.js:79:30)
    at EventEmitter.render (/Users/muradshahmammadli/Desktop/progr/NodeJS/node_modules/express/lib/application.js:570:12)
    at ServerResponse.render (/Users/muradshahmammadli/Desktop/progr/NodeJS/node_modules/express/lib/response.js:971:7)
    at /Users/muradshahmammadli/Desktop/progr/NodeJS/express.js:15:6
    at Layer.handle [as handle_request] (/Users/muradshahmammadli/Desktop/progr/NodeJS/node_modules/express/lib/router/layer.js:95:5)
    at next (/Users/muradshahmammadli/Desktop/progr/NodeJS/node_modules/express/lib/router/route.js:137:13)

我尝试用res.render('home');替换res.send("works");并且它有效,但我需要包含主页,那么我该怎么办?

1 个答案:

答案 0 :(得分:0)

express view engine环境变量接受字符串,而不是对象。您可以在

时定义字符串的含义
app.engine('handlebars', handleBars.engine);

所以,您必须将view engine更改为

app.set('view engine', 'handlebars');