i5ting / koa-generator

Koa' application generator for 1.x and 2.x( Express-style and support all middlewares include async/await )
https://github.com/17koa/koa-generator
MIT License
975 stars 110 forks source link

app.js中routes(router)似乎没对 #59

Closed IanLew closed 5 years ago

IanLew commented 5 years ago

我是初学者,提的可能不对,请原谅。这段代码好像没啥用,又或者是指向的代码有问题。

zhangyuang commented 5 years ago

@IanLew 贴具体代码,app.use(index.routes(), index.allowedMethods());的目的是注册路由

IanLew commented 5 years ago

在生成的app.js中,const routes = require('./routes'),routes目录下有很多路由,然后app.js中有这么段代码routes(router)。我的理解是这段代是为注册routes目录下的所有路由,我不理解的是下面的路由文件都是这样的: module.exports = (router) => { router.get('/welcome', async function (ctx, next) { ctx.state = { title: 'koa2 title' }; await ctx.render('welcome', {title: ctx.state}); }) } 如果我理解没错,这样子并不能成功吧

IanLew commented 5 years ago

之前是照着一个教程做的,装的包是koa2-generator。刚刚我装的包是koa-generator,代码风格和之前的包也不一样。