Security News
Research
Data Theft Repackaged: A Case Study in Malicious Wrapper Packages on npm
The Socket Research Team breaks down a malicious wrapper package that uses obfuscation to harvest credentials and exfiltrate sensitive data.
Template rendering middleware for koa@2
.
npm install koa-views
koa-views
is using @ladjs/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 @ladjs/consolidate
as default engine sourceIf you’re not happy with @ladjs/consolidate
or want more control over the engines, you can override it with this options. engineSource
should
be 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
FAQs
Template rendering middleware for koa
The npm package koa-views receives a total of 29,359 weekly downloads. As such, koa-views popularity was classified as popular.
We found that koa-views demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 4 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Research
The Socket Research Team breaks down a malicious wrapper package that uses obfuscation to harvest credentials and exfiltrate sensitive data.
Research
Security News
Attackers used a malicious npm package typosquatting a popular ESLint plugin to steal sensitive data, execute commands, and exploit developer systems.
Security News
The Ultralytics' PyPI Package was compromised four times in one weekend through GitHub Actions cache poisoning and failure to rotate previously compromised API tokens.