search

Home  >  Q&A  >  body text

javascript - koa uses redirect to jump and it doesn't work. Please let me know why.

//This is the koa startup file
var koa = require('koa');
var path = require('path');
var router = require('koa-router') ();
var server = require('koa-static');

var datas = require('./app/router/datas');
var index = require('./app/router/index');

var app = new koa();

app.use(server(path.join(__dirname, 'app')));

router.use('/',index.routes());
router.use('/datas',datas.routes());

app.use(router.routes());

app.on('error', function(err,ctx){

console.log(err);

});

app.listen(9999,function(){

console.log('服务器已开启,端口9999,浏览器中打开:localhost:9999');

});

//This is the index file, the router.redirect method is used in the index
var router = require('koa-router')();

router.get('/', function() {

console.log('确实已经进来了');
try {
   router.redirect('', 'view/login/login.html');
} catch (error) {
    console.log(error);
}

});

module.exports = router;

When running, when the page enters localhost:9999, the console does print "It has indeed come in", but the page does not jump. Why is this? ? Please help. Xiaobai is learning koa!

高洛峰高洛峰2769 days ago1921

reply all(2)I'll reply

  • 欧阳克

    欧阳克2017-06-26 10:56:50

    I encountered such pitfalls when writing express. At that time, it seemed that the listen did not end. So the program keeps hanging.

    You add router.end() after redirect //koa should have such a method

    reply
    0
  • 世界只因有你

    世界只因有你2017-06-26 10:56:50

    The reason for this problem is due to insufficient understanding of the router.redirect() method. The real usage is:

    router.redirect('back',url)

    'back' is actually a special identifier that represents Referrer. The second parameter is the new URL to jump to.

    reply
    0
  • Cancelreply