Template rendering middleware for koa@2
.
npm install koa-views
koa-views
is using consolidate under the hood.
NOTE: you must still install the engines you wish to use, add them to your package.json dependencies.
var views = require('koa-views');
const render = views(__dirname + '/views', {
map: {
html: 'underscore'
}
})
// Must be used before any router is used
app.use(render)
// OR Expand by app.context
// No order restrictions
// app.context.render = render()
app.use(async function (ctx) {
ctx.state = {
session: this.session,
title: 'app'
};
await ctx.render('user', {
user: 'John'
});
});
For more examples you can take a look at the tests.
If you need to simply render pages with locals, you can install koa-views-render
:
npm install koa-views-render
Then simply use it on your routes and its arguments will be passed to ctx.render
.
var render = require('koa-views-render');
// ...
app.use(render('home', { title : 'Home Page' }));
views(root, opts)
root
: Where your views are located. Must be an absolute path. All rendered views are relative to this path
opts
(optional)
opts.autoRender
: Whether to use ctx.body
to receive the rendered template string. Defaults to true
.
const render = views(__dirname, { autoRender: false, extension: 'pug' });
app.use(render)
// OR
// app.context.render = render()
app.use(async function (ctx) {
return await ctx.render('user.pug')
})
vs.
const render = views(__dirname, { extension: 'pug' })
app.use(render)
// OR
// app.context.render = render()
app.use(async function (ctx) {
await ctx.render('user.pug')
})
opts.extension
: Default extension for your viewsInstead of providing the full file extension you can omit it.
app.use(async function (ctx) {
await ctx.render('user.pug')
})
vs.
const render = views(__dirname, { extension: 'pug' })
app.use(render)
// OR
// app.context.render = render()
app.use(async function (ctx) {
await ctx.render('user')
})
opts.map
: Map a file extension to an engineIn this example, each file ending with .html
will get rendered using the nunjucks
templating engine.
const render = views(__dirname, { map: {html: 'nunjucks' }})
app.use(render)
// OR
// app.context.render = render()
// render `user.html` with nunjucks
app.use(async function (ctx) {
await ctx.render('user.html')
})
opts.engineSource
: replace consolidate as default engine sourceIf you’re not happy with consolidate or want more control over the engines, you can override it with this options. engineSource
shouldbe an object that maps an extension to a function that receives a path and options and returns a promise. In this example templates with the foo
extension will always return bar
.
const render = views(__dirname, { engineSource: {foo: () => Promise.resolve('bar')}})
app.use(render)
// OR
// app.context.render = render()
app.use(async function (ctx) {
await ctx.render('index.foo')
})
opts.options
: These options will get passed to the view engine. This is the time to add partials
and helpers
etc.const app = new Koa()
.use(views(__dirname, {
map: { hbs: 'handlebars' },
options: {
helpers: {
uppercase: (str) => str.toUpperCase()
},
partials: {
subTitle: './my-partial' // requires ./my-partial.hbs
},
cache: true // cache the template string or not
}
}))
.use(function (ctx) {
ctx.state = { title: 'my title', author: 'queckezz' }
return ctx.render('./my-view.hbs')
})
Set the DEBUG
environment variable to koa-views
when starting your server.
$ DEBUG=koa-views
我们做WEB出身的一直很自然的想到怎么实现前端显示,早期我们都是把asp,php在html插入相应的asp及php代码,后面再就是使用一些标记来用于前台显示,主要是为了实现MVC,把前端和后端逻辑分离,现在想像一下按理来nodejs应也是有的,其实koa作为WEB框架按理也是有这一部分的,所今天来学习一下中间件koa-views. 我现在也是一边学一边写的,主要是记录下来当时学习的过程,因此可能有
Koa中常用的中间件: koa-session:让无状态的http拥有状态,基于cookie实现的后台保存信息的session koa-mysql:封装了需要用到的SQL语句 koa-mysql-session:当不想让session存储到内存,而想让session存储到mysql数据库中时使用 koa-router:后台会接受到各种请求的url,路由会根据不同的url来使用不同的处理逻辑。 ko
app.js var Koa = require('koa') var fs = require('fs') var path = require('path') var koaStaticPlus = require('koa-static-plus') var Router = require('./router') var app = new Koa() //处理静态资源 app.use
const Koa = require('koa'); const Router = require('koa-router'); const app = new Koa(); const router = new Router(); router.get('/', function (ctx, next) { ctx.body="Hello JSPang"; }) .get('/todo',(c
Koa中常用的中间件: koa-session:让无状态的http拥有状态,基于cookie实现的后台保存信息的session koa-mysql:封装了需要用到的SQL语句 koa-mysql-session:当不想让session存储到内存,而想让session存储到mysql数据库中时使用 koa-router:后台会接受到各种请求的url,路由会根据不同的url来使用不同的处理逻辑。 ko
koa-views 是一个使用模板引擎渲染视图的 Koa 中间件。它可以集成多种模板引擎,包括 EJS、Pug、Handlebars 等等。 下面是使用 koa-views 渲染 EJS 模板的简单示例: 安装 koa-views 和 EJS: npm install koa-views ejs 引入 koa-views 和 Koa: const Koa = require('koa'); co
1.安装 npm install koa-bodyparser --save 2.引入 // 引入koa-bodyparser插件 let bodyparser = require('koa-bodyparser'); 3.配置 // 配置中间件 app.use(bodyparser()); 4.使用 ctx.request.body;//获取post传过来的参数 案例: app_post.j
Koa art-template view render middleware. support all feature of art-template. Install npm install --save art-template npm install --save koa-art-template Example const Koa = require('koa'); const ren
koa是Express的下一代基于Node.js的web框架,目前有1.x和2.0两个版本。 历史 1. Express Express是第一代最流行的web框架,它对Node.js的http进行了封装,用起来如下: var express = require('express'); var app = express(); app.get('/', function (req, res) {
Koa 是下一代的 Node.js 的 Web 框架。由 Express 团队设计。旨在提供一个更小型、更富有表现力、更可靠的 Web 应用和 API 的开发基础。 Koa可以通过生成器摆脱回调,极大地改进错误处理。Koa核心不绑定任何中间件,但提供了优雅的一组可以快速和愉悦地编写服务器应用的方法。 示例代码: var koa = require('koa');var app = koa();//
Koa - HelloWorld 以上便是全部了,我们重点来看示例,我们只注册一个中间件, Hello Worler Server: <?php $app = new Application(); // ... $app->υse(function(Context $ctx) { $ctx->status = 200; $ctx->body = "<h1>Hello Worl
koa-log4js A wrapper for log4js-node which support Koa logger middleware.Log message is forked from Express (Connect) logger file. Note This branch is use to Koa v2.x.To use Koa v0.x & v1.x, please ch
koa-rudy 环境 node -v >=6.9.0pm2 启动 npm install npm run dev 开发环境 npm run dev || test || prod 接口测试 npm run mocha 推荐开发工具 vscode 实现 支持 async/await MVC架构(middleware-view-controller) RESTful a