queckezz/koa-views

Middleware always defaults to html even if the path has a different extension.

Closed this issue · 5 comments

I have configured koa-views with only the views' path which defaults the middleware to 'html'. But when I use ctx.render('index.ejs') the debugging tries to do this 'index.ejs.html' and gives me an undefined response.
To fix it I include the default option to ejs, which outputs index.ejs.ejs but renders normally.

I'm currently using koa@2, I don't know if this might be part of the problem.

Thanks in advance.

import convert from 'koa-convert';
import {wrap} from 'co';

import views from 'koa-views';

app.use(convert(views(VIEWS_PATH)));

app.use(async (ctx, next) => {
  ctx.render = wrap(ctx.render);
  await next();
});

app.use(async (ctx, next) => {
  await ctx.render('index.ejs', {message: 'Hello world!!!'});
  console.log(ctx.body);
});
"dependencies": {
    "co": "4.6.0",
    "ejs": "2.4.1",
    "koa": "2.0.0-alpha.3",
    "koa-convert": "1.2.0",
    "koa-views": "3.1.0"
  }

Yup definitely a bug, I'll fix that up.

Should be fixed in v4.0.0. Let me know how it works out for you.

In 3.1.0, I use the setup below and it work:

app.use(convert(views('views', {
  root: __dirname + '/views',
  default: 'jade'
})));

router.get('/', (ctx, next) => {
  var title = "Hello World Koa!";
  ctx.render('index', {
    title: title
  });
})

Somehow, 4.0.0 is broken. Even if I add extension: 'jade"

Debug for 3.1.0 (working):
code: https://github.com/niiknow/node-identity/tree/working

  koa-views options: {"root":"/Users/tech/Desktop/git/niiknow/node-identity/app/views","default":"jade"} +0ms
  <-- GET /
  koa-views render `index.jade` with {"title":"Hello World Koa!"} +6s
GET / - 306ms
  --> GET / 200 321ms 1.42kb

Debug for 4.0.0 (not working - include extension: 'jade' because it would default to 'html' if I don't add extension):

code: https://github.com/niiknow/node-identity/tree/iss43-koa-views

  koa-views options: {"root":"/Users/tech/Desktop/git/niiknow/node-identity/app/views","default":"jade","extension":"jade"} +0ms
  <-- GET /
GET / - 2ms
  --> GET / 404 15ms -
  koa-views render `index.jade` with {"title":"Hello World Koa!"} +5s

The root and default option have been removed. You need to provide an absolute path:

app.use(views(__dirname + '/views', { ...opts }))

There is still a slight bug dealing with file extension whether the file should be sent as is or rendered. I've uploaded a pull request to deal with it. #44