koa2系列教程,持续更新koa2系列教程:初看koa2应用koa2系列教程:koa2处理静态文件koa2系列教程:koa2使用模板引擎koa2系列教程:koa2路由控制中间件系列教程:koa2实现登录注册功能本文源码地址:https://github.com/xiaqijian/...本文是前几天内容的综合,使用koa静态文件处理,路由,模板引擎版本:项目结构如下:1.写路由控件//router/index.jsconstRouter=require('koa-router')//子路由1consthome=newRouter()home.get('/',async(ctx)=>{lettitle='Home'awaitctx.render('index',{title})})//子路由2constpage=newRouter()page.get('/404',async(ctx)=>{lettitle="404"awaitctx.render('err',{title})})constlogin=newRouter()login.get('/',async(ctx)=>{lettitle="login"awaitctx.render('login',{title})})constregister=newRouter()register.get('/',async(ctx)=>{lettitle="register"等待ctx。render('register',{title})})//加载所有子路由letrouter=newRouter()router.use('/',home.routes(),home.allowedMethods())router.use('/page',page.routes(),page.allowedMethods())router.use('/login',login.routes(),login.allowedMethods())router.use('/register',register.routes(),register.allowedMethods())module.exports=路由器2。写ejs,可以自己写,随意,想看我写的可以看开头的源码地址3.index.jswrite//index.jsconstkoa=require('koa')constviews=require('koa-views')conststatics=require('koa-static')constpath=require('path')constrouter=require('./router')constapp=newKoa()conststaticPath='./static'app.use(statics(path.join(__dirname,staticPath)))//加载模板引擎app.use(views(path.join(__dirname,'./views'),{extension:'ejs'}))//加载路由中间件app.use(router.routes()).use(router.allowedMethods())app.listen(3000,()=>{console.log('localhost:3000')})4。启动服务节点index.js,打开浏览器:localhost:3000localhost:3000/loginlocalhost:3000/register本篇到此结束,下篇将使用MongoDB实现一个登录注册功能