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?
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!
– Franck Costa
@Franckcosta feel free to share the problem, so I change the answer to help other friends in the future.
– Hiago Souza
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.
– Franck Costa
Ah yes nice! Good codada ai bro ;)
– Hiago Souza