Does anyone know how to resolve Error: "Failed to lookup view" in nodejs?

Asked

Viewed 2,953 times

1

My folders:

inserir a descrição da imagem aqui

The "admin.js" file contains my routes:

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

router.get('/', (req,res)=>{
    res.render("admin/index")
})

router.get('/posts',(req,res)=>{

})

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

module.exports = router

The file "App.js"

const express = require('express')
    const  handlebars = require('express-handlebars')
    const bodyParcer =  require('body-parser')
    const app = express()
    const admin = require('./routes/admin')
    const path = require('path')
    //const  mongoose = require('mongoose')


    //Body Parcer
        app.use(bodyParcer.urlencoded({extended:true}))
        app.use(bodyParcer.json())

    //HandleBars

        app.engine('handlebars', handlebars({defaultLayout: 'main'}))
       app.set('view engine', 'handlebars')


    //Mongoose


    //Public
        app.use(express.static(path.join(__dirname,"public")))
//Rotas

app.use('/admin',admin)



const PORT = 8080
app.listen(PORT,()=>console.log(`Servidor Rodando na porta ${PORT} ...`))

When I try to access the url "http://localhost:8080/admin/" to access the "index.handlebars" file in the "views/admin" folder, the following error occurs:


Error: Failed to lookup view "admin/index" in views directory "C:\workspaceN\BlogApp\views"

  • if you notice the image you will see that its written "index.hadlebars" and the right one would be "index.

1 answer

3


By the description of the error, it is not locating the admin/index path because it is inside the views folder, it would have to inform this path. Above the line where you activate the app.engine('handlebars'), you can put the following:

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

Here is a tutorial that explains the handlebars configuration

https://www.oodlestechnologies.com/blogs/How-To-Use-Handlebars-in-Node-JS

Browser other questions tagged

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