I use koa.js and there is a problem: how can I make the system render a new template or just redirect when receiving post data?

Several times I tried to do something like this, but nothing happens. I can not understand why.

var server = require('koa')(), path = require('path'), projectRoot = __dirname, staticRoot = path.join(projectRoot, '../public'), templateRoot = path.join(projectRoot, '/engine/template'), koaviews = require('koa-views')(templateRoot, {default : 'swig'}), body = require('koa-better-body'), router = require('koa-router')(); server.use(require('koa-static')(staticRoot)); server.use(koaviews); server.use(body({ multipart: true })); router.get('/', function *(next) { console.log('GET'); yield this.render('terminal/testlogin'); }); router.post('/', function *(next) { console.log('POST'); yield this.render('terminal/terminal'); }); server.use(router.routes()); /*server.use(function *() { this.status = 404; yield this.render('service/404'); });*/ server.listen(80); console.log('Сервер запустился. Порт - 80'); 

get works successfully - the form is displayed, but on post data when sending from a form, I get only an entry in the console and that's it. The page does not change.

Also tried to do this:

 router.post('/', function *(next) { console.log('POST'); this.redirect('/sign-in'); this.status = 301; }); 

Nothing too. When trying to pass on through yield

 yield this.redirect('/sign-in'); 

I get notified that only generators or promises can be transmitted.

    1 answer 1

    The following code is used for redirections to koa:

     var app = require('koa')(), router = require('koa-router')(); // Этот маршрут нужен только для того, чтобы сгенерировать // корректный POST запрос на сервер. router.get('/', function *() { this.body = '<html><head></head><body>' + '<form method="POST"><input type="submit" /></form>' + '</body></html>'; }); router.post('/', function *(){ this.status = 301; this.redirect('/home'); }); router.get('/home', function *() { this.body = 'This is the home'; }); app.use(router.routes()); app.listen(8000); 

    And this code, which is typical, works.

    Using

     yield this.redirect('/sign-in'); 

    does not make sense, because this.redirect does not return either a Promise generator (as reported to you by koa in the error text).

    As a result, your error is likely to be somewhere outside the provided code. Without a minimal test case that would lead to an error, nothing more specific can be said.