//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!
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:
'back' is actually a special identifier that represents Referrer. The second parameter is the new URL to jump to.