routing-controllers icon indicating copy to clipboard operation
routing-controllers copied to clipboard

Help with Render()

Open LANWrench opened this issue 3 years ago • 2 comments

I was trying to... I am trying to use Render() with EJS.

The problem: First off I want to thank you for this library, it really awesome and have really enjoyed using it.

I have a new project I am working on and decided that EJS would be best for it. I am using the Render decorator and the first call it works perfectly but any subsequent calls that use a different template will get the first template returned.

For example, if I request /puppies it will return the template from puppies.ejs. If I then request /kittens, debug shows the template file for puppies still being returned.

I am using the koa-ejs module with this, but have tested with koa-views and get the same result.

Here are my files:

// server.ts
import { createKoaServer } from 'routing-controllers';
import Koa from 'koa';
import * as HttpStatus from 'http-status-codes';
import render from 'koa-ejs';
import * as path from 'path';
import 'reflect-metadata';

const app: Koa = createKoaServer({
  controllers: [__dirname + "/controllers/**/*{.ts,.js}"]
});

render(app, {
  root: path.join(__dirname, 'views'),
  layout: 'layout',
  viewExt: 'ejs',
  cache: false,
  debug: true
});

app.listen(3001), () => {
  console.log(`Server listening on port 3001`)
};



// kittens.controller.ts
import {Controller, Get, Render } from 'routing-controllers';

@Controller('/kittens')
export class KittensController {
    constructor(){}

    @Render('kittens')
    @Get()
    getKittens() {
        return {
            animal: 'kittens'
        }
    }
}



//puppies.controller.ts
import {Controller, Get, Render} from 'routing-controllers';

@Controller('/puppies')
export class PuppiesController {
    constructor(){}

    @Get()
    @Render('puppies')
    getPuppies() {
        return {
            animal: 'puppies'
        }
    }
}

Templates:

<!-- layout.ejs -->
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>EJS Test</title>
</head>
<body>

  <%- body %>

</body>
</html>



<!-- puppies.ejs -->
This is the puppies  page.


<!-- kittens.ejs -->
This is the kittens  page.

I am a network engineer by day, learning app development by night so if my code doesn't look good feel free to point it out.

Thank you very much for your time and any help you may be able to provide.

LANWrench avatar Aug 18 '20 14:08 LANWrench

Same Problem

AnthonyACE avatar Sep 29 '21 09:09 AnthonyACE

I had the same problem and have fixed it:

https://github.com/typestack/routing-controllers/pull/780

nanyuantingfeng avatar Oct 01 '21 05:10 nanyuantingfeng