problems with routes in JS + handlebars (Referenceerror: home is not defined)

Asked

Viewed 26 times

-1

I’m learning Ode.js, but I’m getting too caught up in establishing the routes correctly.

The structure of my folder is currently like this

estrutura das pastas

The code of the file server.js is thus found:

const express = require('express')
const app = express()
const hbs = require('express-handlebars')
const path = require('path')
const pdf = require('html-pdf')

//template
//app.engine('handlebars',hbs())
app.engine('handlebars',hbs({
    layoutsDir: __dirname + '/views/layouts',
    }));

app.set('view engine', 'handlebars')
app.set('views', __dirname + '/views')
//app.set('views', path.join(__dirname, 'views'))

app.get('/', function(req, res){
    res.render('home', {layout: 'main'})
    pdf.create(home, options).toFile("conclusao.pdf", (err, data)=>{
        return response.send("Erro ao gerar o PDF")
    })
})

const discentes = [
    {
        name: "Suaziele Cristina de Souza",
        age: 21,
        courseData: "21/12/2020",
        date: "10/06/2021",  
        obs: "Graduada em Ciência da Computaçâo."
    },
];

app.listen(3000)

The intention was to connect the "home.handlebars" home page and display its content.

NOTE: the file "main.handlebars" has only this code

{{{body}}}

Thanks in advance for the attention of this community that has helped me so much and continues to help those who study programming.

  • pdf.create( But here you want to convert a page into pdf. html page has a look at this respposta https://stackoverflow.com/a/37299711/6754506. And this home has not been declared anywhere else

1 answer

0

Create a folder with the name of pages inside the views, move home.handlebars into it, and test that code, what I did was just change the layout of the handlebars and render it.


const express = require('express')
const app = express()
const handlebars = require('express-handlebars')
const path = require('path')
const pdf = require('html-pdf')

//template
//app.engine('handlebars',hbs())
app.engine('handlebars',handlebars({
     defaultLayout: "main"
    }));

app.set('view engine', 'handlebars')

//app.set('views', path.join(__dirname, 'views'))

app.get('/', function(req, res){
    res.render("pages/home")
    pdf.create(home, options).toFile("conclusao.pdf", (err, data)=>{
        return response.send("Erro ao gerar o PDF")
    })
})

const discentes = [
    {
        name: "Suaziele Cristina de Souza",
        age: 21,
        courseData: "21/12/2020",
        date: "10/06/2021",  
        obs: "Graduada em Ciência da Computaçâo."
    },
];

app.listen(3000)

Test and see if it works now

Browser other questions tagged

You are not signed in. Login or sign up in order to post.