nestjs / nest

A progressive Node.js framework for building efficient, scalable, and enterprise-grade server-side applications with TypeScript/JavaScript 🚀
https://nestjs.com
MIT License
67.32k stars 7.59k forks source link

Use Pug in Nest #115

Closed franciscoffavaro closed 7 years ago

franciscoffavaro commented 7 years ago

I need to use pug in my projects, and i have this problem:

renderRecibo = (req, res, next) =>{
    try{
      var options = { format: 'A4' };
      let html = pug.renderFile("../../views/recibo.pug"

If i use only Node, works, but in Nest no.

In main file of Nest, i have:

app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'pug');

thanks for all help.

franciscoffavaro commented 7 years ago

renderRecibo = (req, res, next) =>{ try{ var options = { format: 'A4' }; let html = pug.renderFile("../../views/recibo.pug", { title: 'Recibo de @Pagamento', image: '596f619cc9f73b0613594a49' }); pdf.create(html, options).toStream((err, stream)=>{ stream.pipe(res); }) }catch(e){ console.log(e) } }

this is the complete code.

cdiaz commented 7 years ago

Hi @franciscoffavaro, i use Pug in this example and it works perfect

franciscoffavaro commented 7 years ago

Hi @cdiaz, it's the same code of i have in my app. The problem is, i have a method called renderRecido. I get a pug file and render to make a pdf file and show. But all time, the file path send me an error:

{ Error: ENOENT: no such file or directory, open '../../views/recibo.pug'
    at Error (native)
    at Object.fs.openSync (fs.js:641:18)
    at Object.fs.readFileSync (fs.js:509:33)
    at handleTemplateCache (/Users/franciscofavaro/Documents/Github/nest-backend/node_modules/pug/lib/index.js:215:37)
    at Object.exports.renderFile (/Users/franciscofavaro/Documents/Github/nest-backend/node_modules/pug/lib/index.js:428:10)
    at ImagesService.renderRecibo.e [as renderRecibo] (/Users/franciscofavaro/Documents/Github/nest-backend/src/modules/images/images.service.ts:31:22)
    at ImagesController.getRecibo (/Users/franciscofavaro/Documents/Github/nest-backend/src/modules/images/images.controller.ts:14:26)
    at e (/Users/franciscofavaro/Documents/Github/nest-backend/node_modules/@nestjs/core/router/router-proxy.js:7:33)
    at Layer.handle [as handle_request] (/Users/franciscofavaro/Documents/Github/nest-backend/node_modules/@nestjs/core/node_modules/express/lib/router/layer.js:95:5)
    at next (/Users/franciscofavaro/Documents/Github/nest-backend/node_modules/@nestjs/core/node_modules/express/lib/router/route.js:131:13)
  errno: -2,
  code: 'ENOENT',
  syscall: 'open',
  path: '../../views/recibo.pug' }

Not found the file, but the path is right. I really don't know whats happening.

cdiaz commented 7 years ago

Try render template without path or extension, check this

franciscoffavaro commented 7 years ago

Hi again @cdiaz. I need to render a file, because i use their to generate a pdf file. The render File is save in a variable, and this variable is transformed in a pdf file.


renderRecibo = (req, res, next) =>{
    try{
      var options = { format: 'A4' };
      let html = pug.renderFile('../../views/recibo.pug', {
      title: 'Recibo de Pagamento',
      nome: 'Francisco Favaro',
      valor: '1234,56',
      metragem: '200',
      quadra: 'D36F',
      numero: '02',
      loteamento: 'Cidade Balneário Novo Mundo',
      day: '01',
      month: 'Janiero',
      year: '2015',
      valor_literal: 'Hum Mil Reais',
      nome_empresa: 'ARQUING CONSULTORES LTDA-ME',
      image: '596f619cc9f73b0613594a49'
    });
    pdf.create(html, options).toStream((err, stream)=>{
      stream.pipe(res);
    })
  }catch(e){
    console.log(e)
  }
  }

This is the complete code. I use the pug function to create a HTML file and send to screen in PDF format.

franciscoffavaro commented 7 years ago

the only form to make this work is using the complete path... The simple solution is: let html = pug.renderFile('/Users/franciscofavaro/Documents/Github/nest-backend/src/views/recibo.pug', ...)

lock[bot] commented 5 years ago

This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.