javascript - koa uses redirect to jump and it doesn't work. Please let me know why.
高洛峰
高洛峰 2017-06-26 10:54:50
0
2
1915

//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!

高洛峰
高洛峰

拥有18年软件开发和IT教学经验。曾任多家上市公司技术总监、架构师、项目经理、高级软件工程师等职务。 网络人气名人讲师,...

reply all(2)
学霸

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

世界只因有你

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.

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template