queckezz/koa-views

AssertionError: app.use() requires a generator function

anirbanroydas opened this issue · 4 comments

I am using the koa-views in the exact way as mentioned in the README. But somehow its giving me the following assertion error:

assert.js:85
  throw new assert.AssertionError({
  ^
AssertionError: app.use() requires a generator function
    at Application.app.use ($PROJECT_DIR/koatest/node_modules/koa/lib/application.js:106:5)
    at Object.<anonymous> ($PROJECT_DIR/koatest/koatest/server.js:23:5)
    at Module._compile (module.js:556:32)
    at Object.Module._extensions..js (module.js:565:10)
    at Module.load (module.js:473:32)
    at tryModuleLoad (module.js:432:12)
    at Function.Module._load (module.js:424:3)
    at Module.runMain (module.js:590:10)
    at run (bootstrap_node.js:394:7)
    at startup (bootstrap_node.js:149:9)
    at bootstrap_node.js:509:3

Versions Info:

  • node@6.5.0
  • koa@1.2.4
  • koa-router@5.4.0
  • koa-views@5.1.2

I have a mvc design. I am furnishing my code below:

server.js

const koa = require('koa');
const app = koa();
const views = require('koa-views'); 

// Must be used before any router is used
app.use(views(__dirname + '/src/client/templates', { extension: 'html' })); 

const indexApi = require('./src/backend/routes/indexRoute');
const chatApi = require('./src/backend/routes/chatRoute');

// Use the routes 
app.use(indexApi.routes());
app.use(indexApi.allowedMethods()); 
app.use(chatApi.routes());
app.use(chatApi.allowedMethods()); 


// start server / start listening
app.listen(8080, () =>{
    console.log('App started at 127:0.0.1:8080');
});

indexRoute.js

const Router = require('koa-router'); 
const indexView = require('../views/indexView');

const api = Router();

api.get('/', indexView.IndexHandler); 

module.exports = api;

chatRoute.js

const Router = require('koa-router'); 
const chatView = require('../views/chatView');

const api = Router();

api.get('/chat', chatView.ChatHandler); 

module.exports = api;

indexView.js

// Handler/Controller to handle route requests to index page
let IndexHandler = function *(next) {
    console.log('Entered IndexHandler'); 
    this.state = {
        session: this.session,
        title: 'koatest index'
    };

    // yield this.render('index');
    this.body = 'Ths is indexHanler';
}

exports.IndexHandler = IndexHandler;

chatView.js

// Handler/Controller to handle route requests to index page
let ChatHandler = function *(next) {
    console.log('Entered ChatHandler'); 
    this.state = {
        session: this.session,
        title: 'koatest chat'
    };

    // yield this.render('chat', {user: 'John'});
    this.body = 'this is ChatHandler';
}

exports.ChatHandler = ChatHandler;

NOTE : If I don't use the koa-views middleware, my server runs absolutely fine with all the router functioning properly.

What am I missing here?

You are using koa@1.x but koa-views@5.x needs koa@2 or above. If you are still at v1 please consider using koa-views@4.x. Note however, there are no updates supporting v1

@queckezz : But koa@2 supports async/await, but its still not supported under bare node js. Also, the generators are a great concept, why the decision not to support koa@1.x ?

I don't have time to simultaneously support both versions and had to make the decision to support only one. Since people seemed to move on to @2 I decided to go for @2 maintenance only. However, koa-views@4 should be stable as is, and can still be used.

@queckezz Okay. Thanks for the quick response.