Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
491 views
in Technique[技术] by (71.8m points)

javascript - I am getting this error :-Error: Failed to lookup view "index" in views directory please tell me the solution

app.js it is main file

// importing the module
const express = require("express")
const path = require("path")
const app = express()
require("./db/conn")

// defining a localhost port number  
const port = process.env.PORT || 3000

// defining and using static files(i.e html)
app.use(express.static(path.join(__dirname, "../public")))

// defining handle bar engine
app.set("view engine", "hbs")

// this is just for test
app.get("/", (req, res) => {
    res.render("index")
})

// creating our localhost
app.listen(port, () => {
    console.log("Connection Successfull");
})

this is my index.hbs file

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    <h1>Hello World!</h1>
</body>
</html>

error

Error: Failed to lookup view "index" in views directory "D:ProgramsProjectsmern_projectsrcviews"
  at Function.render (D:ProgramsProjectsmern_project
ode_modulesexpresslibapplication.js:580:17)
  at ServerResponse.render (D:ProgramsProjectsmern_project
ode_modulesexpresslib
esponse.js:1012:7)
  at D:ProgramsProjectsmern_projectsrcapp.js:18:9
  at Layer.handle [as handle_request] (D:ProgramsProjectsmern_project
ode_modulesexpresslib
outerlayer.js:95:5)
  at next (D:ProgramsProjectsmern_project
ode_modulesexpresslib
outer
oute.js:137:13)
  at Route.dispatch (D:ProgramsProjectsmern_project
ode_modulesexpresslib
outer
oute.js:112:3)
  at Layer.handle [as handle_request] (D:ProgramsProjectsmern_project
ode_modulesexpresslib
outerlayer.js:95:5)
  at D:ProgramsProjectsmern_project
ode_modulesexpresslib
outerindex.js:281:22
  at Function.process_params (D:ProgramsProjectsmern_project
ode_modulesexpresslib
outerindex.js:335:12)
  at SendStream.error (D:ProgramsProjectsmern_project
ode_modulesserve-staticindex.js:121:7)
  at SendStream.emit (events.js:315:20)
  at SendStream.error (D:ProgramsProjectsmern_project
ode_modulessendindex.js:270:17)
  at SendStream.onStatError (D:ProgramsProjectsmern_project
ode_modulessendindex.js:421:12)
  at next (D:ProgramsProjectsmern_project
ode_modulessendindex.js:763:28)
  at D:ProgramsProjectsmern_project
ode_modulessendindex.js:771:23
question from:https://stackoverflow.com/questions/65877741/i-am-getting-this-error-error-failed-to-lookup-view-index-in-views-director

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

set view property

app.set('views', __dirname + '/views');

before setting view engine

Example

const port = process.env.PORT || 3000

// defining and using static files(i.e html)
app.use(express.static(path.join(__dirname, "../public")))

//engine added after editing answer
app.engine('.hbs', exphbs({ defaultLayout: 'main', extname: '.hbs' }));

// necessary for templates
app.set('views', __dirname + '/views');

// defining handle bar engine
app.set("view engine", "hbs")

// this is just for test
app.get("/", (req, res) => {
    res.render("index")
})

Edited to add: Hbs engine was not declared. Should be before set view

app.engine('.hbs', exphbs({ defaultLayout: 'main', extname: '.hbs' }));

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...