res.render does not load . handlerbars within route - Node.JS and Handlerbars

Asked

Viewed 283 times

2

Good night!

I’m learning Node.JS and I’m stuck on the routes.

I have the following codes.

//app.js

// carregando modulos
const express = require('express')
const handlebars = require('express-handlebars')
const bodyParser = require('body-parser')
const app = express();
const admin = require('./routes/admin')
const path = require('path')

// const mongoose = require('mongoose')

// Configs
// body parser
    app.use(bodyParser.urlencoded({extended:true}))
    app.set('view engine','handlebars')
    app.set('views', __dirname + '/views');
// mongoose

// Public  
    app.use(express.static(path.join(__dirname,'public')))
// Routes
app.get('/', (req, res)=>{
    res.send('Rota principal!')

})
app.get('/posts', (req, res)=>{
    res.send('Lista Posts')
})
app.use('/admin',admin)
// Others
const PORT = 3000;
app.listen(PORT, ()=>{
    console.log("Server Run!");
})

//routes/admin.js

const express = require('express')
const router = express.Router()

router.get('/',(req, res)=>{
    res.render('partials/_navbar')
})

router.get('/posts',(req, res)=>{
    res.send('Página de posts')
})

router.get('/categorias', (req, res)=>{
    res.send("Página categorias")
})

module.exports = router

and receiving the following error

Error: Module "handlebars" does not provide a view engine.
at new View (C:\xampp\htdocs\blogapp\node_modules\express\lib\view.js:84:13)
at Function.render (C:\xampp\htdocs\blogapp\node_modules\express\lib\application.js:570:12)
at ServerResponse.render (C:\xampp\htdocs\blogapp\node_modules\express\lib\response.js:1012:7)
at C:\xampp\htdocs\blogapp\routes\admin.js:5:9
at Layer.handle [as handle_request] (C:\xampp\htdocs\blogapp\node_modules\express\lib\router\layer.js:95:5)
at next (C:\xampp\htdocs\blogapp\node_modules\express\lib\router\route.js:137:13)
at Route.dispatch (C:\xampp\htdocs\blogapp\node_modules\express\lib\router\route.js:112:3)
at Layer.handle [as handle_request] (C:\xampp\htdocs\blogapp\node_modules\express\lib\router\layer.js:95:5)
at C:\xampp\htdocs\blogapp\node_modules\express\lib\router\index.js:281:22
at Function.process_params (C:\xampp\htdocs\blogapp\node_modules\express\lib\router\index.js:335:12)

the intention here is to use the layout calling the res.render.

Folder structure

--blogapp
  --routes
  --views
    --layout
    --admin

I’m following this tutorial, Node.js Course - Static Files #33

Can you help me?

1 answer

2


Following the documentation of lib, managed to play your problem. It turns out you need to set the view engine using the method app.engine.

To solve the problem, just add the content app.engine('handlebars', handlebars()); before the line app.set('view engine','handlebars').

Follows code:

//app.js

// carregando modulos
const express = require('express')
const handlebars = require('express-handlebars')
const bodyParser = require('body-parser')
const app = express();
const admin = require('./routes/admin')
const path = require('path')

// const mongoose = require('mongoose')

// Configs
// body parser
    app.use(bodyParser.urlencoded({extended:true}))
    app.engine('handlebars', handlebars());
    app.set('view engine','handlebars')
    app.set('views', __dirname + '/views');
// mongoose

// Public  
    app.use(express.static(path.join(__dirname,'public')))
// Routes
app.get('/', (req, res)=>{
    res.send('Rota principal!')

})
app.get('/posts', (req, res)=>{
    res.send('Lista Posts')
})
app.use('/admin',admin)
// Others
const PORT = 3000;
app.listen(PORT, ()=>{
    console.log("Server Run!");
})

//routes/admin.js

const express = require('express')
const router = express.Router()

router.get('/',(req, res)=>{
    res.render('partials/_navbar')
})

router.get('/posts',(req, res)=>{
    res.send('Página de posts')
})

router.get('/categorias', (req, res)=>{
    res.send("Página categorias")
})

module.exports = router

Important: Remember that I did not test your code, but I was able to reproduce the error mentioned by creating a new project from scratch and removing this line (which is the only instruction not implemented in your code but contained in the lib documentation).

  • 1

    Dude, it gave a problem even with this solution, but I needed to rename the layout folder to layouts, which was wrong too, and it worked. It worked great, thanks!

  • @Franckcosta feel free to share the problem, so I change the answer to help other friends in the future.

  • Your solution is correct, there was another problem that I had not noticed that the layout folder was not named correctly, which should be layouts.

  • Ah yes nice! Good codada ai bro ;)

Browser other questions tagged

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