koa-views | Template rendering middleware for koa | Application Framework library
kandi X-RAY | koa-views Summary
kandi X-RAY | koa-views Summary
Template rendering middleware for koa (hbs, swig, pug, anything! :sparkles:)
Support
Quality
Security
License
Reuse
Top functions reviewed by kandi - BETA
- Middleware to render middleware
- Render page .
- Detects if ext is an HTML extension .
koa-views Key Features
koa-views Examples and Code Snippets
Community Discussions
Trending Discussions on koa-views
QUESTION
I want to add custom template functions for Pug views.
For simplicity I've created a demo file with one custom function uppercase
:
ANSWER
Answered 2020-Jan-06 at 21:40Not using Koa, but I solved a similar issue recently using Eleventy.js with Pug. The Pug documentation is not great on this, so it took a while to figure out.
I had to make the function a global, then use the globals property when rendering the template.
e.g.
QUESTION
The full code for this project can be found here: https://github.com/AlexMercedCoder/KoaStarterBlog
The Video series I was making with this can be found here so you can see how things worked at different stages: https://www.youtube.com/watch?v=8_aWw7lfKKI&list=PLY6oTPmKnKbbF4t0Y9DcUVYi7f4kix7Qj
I actually illustrate the problem in the beginning part of this video and walk through the construction of all my routes: https://youtu.be/ltAxokJsaWE
So I built out this basic blog app using KoaJS. When I initially run the index.js the behavior is as follows. - Root Route Works - Create route works - Admin route works - The Delete Button works on the admin page - The edit route just doesn't not (works on and off even though the code is just like the other routes)
Bigger Problem: After submitting a form by hitting the delete button or by creating a new post all the routes except the create route stop working and instead they just return (not found). At first I thought this was a problem being caused by ctx.redirect because they would always fail and be followed by the broken routes but while rendering a complete page seems to work initially typing root or admin route into the browser after form submission still breaks.
*Update: this happens after going to any route, every route works if its the first route accessed but then all other routes except create stop working afterwards. It's as if the first route creates some sort of limbo. The weird thing is the router still console logs everything the route should do up until either a ctx.render, ctx.redirect or ctx.body is to be returned.
below is the index.js code!
...ANSWER
Answered 2019-Nov-05 at 11:13It looks like a http-server fault. Try to add an error handler.
Also I recomment to change error handling in code e.g.
QUESTION
I try to write a web page, and it should support multi-language, in index.js
ANSWER
Answered 2018-May-02 at 20:19const Koa = require('koa');
const views = require('koa-views');
const path = require('path');
app.use(views(path.join(__dirname, './view'), {
extension: 'ejs'
}));
const siteTitleLibrary = {
"en": {
"site": {
"title": "Title"
}
},
"de": {
"site": {
"title": "Titel"
}
}
};
// or
// const siteTitleLibrary = require('language.json');
app.use( async ( ctx ) => {
await ctx.render('index', {
site: siteTitleLibrary[language]
});
});
app.listen(3000, ()=>{
console.log('app runs on port 3000');
});
QUESTION
This is a weird bug so I must be missing something obvious, but here it is.
I'm trying to set up a Koa server to serve several Koa apps depending on the vhost name. The entry point is server.js
:
ANSWER
Answered 2018-Jan-24 at 11:40Finally I think found what the problem was, and, as it happens, the essential piece of info was missing in my question.
The ./apps/app1
folder has its own node_modules
, with its own copy of Koa. Therefore, server.js
and apps/app1/index.js
each have their own, different, Koa.
So I suppose that in the line: const middleware = app instanceof Koa ? app.middleware : app;
, app instanceof Koa
will always return false for that reason.
One solution is simply to remove Koa from the app's node_modules, so Koa is inherited from the outer folder. (At first sight, it has some drawbacks for me because I would like to the apps to be standalone).
But I think I'll just skip the instanceof Koa
test and have const middleware = app.middleware;
instead (I borrowed the original line from https://github.com/koajs/examples/blob/master/vhost/app.js#L14).
QUESTION
I'm trying to build a HMR for koa2 development with chokidar
.
Changing text in ./middlewares/render
triggers chokidar file-watch event, and require.cache cleared immediately as expected, but when I reload page, the text rendered actually not changed.
ANSWER
Answered 2017-Jul-28 at 09:46Okay... This repository helped.
PreviouslyQUESTION
If I send POST such /image/cover
or /image/sub/
from client, the router function doesn't work at all so It sends 404. It's supposed to work but I literally have no idea. I never had this case It just doesn't work for no reason.
router
...ANSWER
Answered 2017-Jul-18 at 00:13you need help function:
// @help function record route table map
QUESTION
So I started my new project and a friend of mine conveiced me to ditch PHP and give Nodejs a try with rethinkDB.
I installed everything and without routes, it works. But once I add the routes, I got : Not Found
with no error on the console.
Packages:
...ANSWER
Answered 2017-Jun-25 at 16:19so this should work: for the index.js change it to:
QUESTION
I installed koa, then installed koa-views. After that I copied example code from the koa-views docs and pasted it into index.js
...ANSWER
Answered 2017-Feb-11 at 21:57Try installing npm install koa-views@4.1.0
seems like there might be something different with the new version. See here for where I got the solution.
I tried it, and I no longer got the error.
Community Discussions, Code Snippets contain sources that include Stack Exchange Network
Vulnerabilities
No vulnerabilities reported
Install koa-views
Support
Reuse Trending Solutions
Find, review, and download reusable Libraries, Code Snippets, Cloud APIs from over 650 million Knowledge Items
Find more librariesStay Updated
Subscribe to our newsletter for trending solutions and developer bootcamps
Share this Page