var mount = require(‘koa-mount‘);//用于路由 var koa = require(‘koa‘); var views = require(‘koa-views‘);//用于界面渲染 // hello.html
var kk = require(‘koa‘); var aa = kk(); aa.use(function *(next){ yield this.render(‘./hello‘); }); module.exports = aa //hello.js文件可以逻辑分离出来的
var a = koa(); a.use(function *(next){ yield this.render(‘./hello‘); }); // world.html var b = koa(); b.use(function *(next){ yield this.render(‘./world‘) }); //basic.html var c = koa(); c.use(function *(next){ yield this.render(‘./basic‘) }); var app = module.exports=koa();//module.exports暂时的理解就是如果写了这个那么别的js导入的时候就是这个对象 app.use(function *PageNotFound(next){ yield next; if (404 != this.status) return; this.status = 404; yield this.render(‘./notFound‘) }) app.use(views()); app.use(mount(‘/hello‘, a)); app.use(mount(‘/world‘, b)); //app.use(mount(‘/‘,c)); //不知道为什么这个是通配符(如果打开这个测试不到404) app.listen(3000); console.log(‘listening on port 3000‘);
时间: 2024-10-27 02:14:55