无法读取属性'渲染'未定义的

时间:2017-08-15 05:53:21

标签: node.js koa

我正在koa.js制作一个简单的程序,其中我试图将信息呈现到html页面即index.html,每当我试图运行代码时它都会给出错误..我厌倦了这个东西,仍然无法找到更好的解决方案..

TypeError:无法读取属性'渲染'未定义的   在Object.render(C:\ Users \ hamza \ Desktop \ TheFirstKoaApp \ Task3 \ app.js:27:8)   在下一个(本地)   在Object.dispatch(C:\ Users \ hamza \ Desktop \ TheFirstKoaApp \ node_modules \ KOA-路由器\ lib中\ router.js:331:14)       在下一个(本地)       在onFulfilled(C:\ Users \ hamza \ Desktop \ TheFirstKoaApp \ node_modules \ co \ index.js:65:19)       在C:\ Users \ hamza \ Desktop \ TheFirstKoaApp \ node_modules \ co \ index.js:54:5       在Object.co(C:\ Users \ hamza \ Desktop \ TheFirstKoaApp \ node_modules \ co \ index.js:50:10)       at Object.toPromise(C:\ Users \ hamza \ Desktop \ TheFirstKoaApp \ node_modules \ co \ index.js:119:63)       在下一个(C:\ Users \ hamza \ Desktop \ TheFirstKoaApp \ node_modules \ co \ index.js:100:29)       在onFulfilled(C:\ Users \ hamza \ Desktop \ TheFirstKoaApp \ node_modules \ co \ index.js:70:7)       at process._tickCallback(node.js:368:9)

这是我的代码..

app.js:

const koa = require('koa');
const router = require('koa-router');
const config = require(__dirname+"/config.js");
const app = new koa();

 var route = router();
app.use(route.routes());

var views = require("co-views");
 var render = views("views", { map: { html: 'swig' }});

route.all("/hi", function *(req,res){
    var myUser = {name:"john"};
    res.render ("index",{user:myUser});
  //this.body = yield render("index");
}); 

startKoa();
function startKoa() {
    app.listen(config.koa.port);
    console.log('Listening on port '+config.koa.port);
}

的index.html:

<html>

<body>

<script>
  var username = "{{user.name}}";
</script>

<script src="app.js"></script>
</body>

</html>

0 个答案:

没有答案