queckezz/koa-views

Error: ENOENT: no such file or directory

tangdaohai opened this issue · 0 comments

I use koa-views render html and ejs.

  1. first url = localhost:3333/test1(will render ejs), It works great.
  2. I change url to /test2 (will render html), It works great.
  3. but when I change url to /test1, It not work……

My code:

var koa = require("koa");
var views = require("koa-views");

var app = new koa();

app.use((ctx, next) => {
  const start = new Date();
  return next().then(() => {
    const ms = new Date() - start;
    console.log(`${ctx.method} ${ctx.url} - ${ms}ms`);
  });
});

app.use(views(__dirname + "/views", { extension: "ejs" }));

app.use( (ctx, next) => {
    if("/test2" === ctx.path){
        return ctx.render("test2.html"); //render test2.html
    }
    return next();
});

app.use( (ctx, next) => {
    if("/test1" === ctx.path){
        return ctx.render("test1"); //render test1.ejs
    }
    return next();
});

app.listen(3333);