'Error: Failed to lookup view "layout" in views directory

I am getting the error like Error: Failed to lookup view "layout" in views directory. Below is the source code of the files as well as the folder structure.

index.ejs

<!DOCTYPE html>
<html>
<head>
    <title>Form Handling</title>
</head>
<body>
    <h1>Hello World</h1>
</body>
</html>

server.js

//require express
var express = require('express')
var app = express()
var expressLayouts = require('express-ejs-layouts')

//Setting the View Engine as EJS
app.set('view engine', 'ejs')
app.use(expressLayouts)

//Specifying the router file
var router = require('./routes')
app.use('/', router)

//Starting a server on port 8004
app.listen(8004, function () {
    console.log('Server is running at port 8004...!');
})

routes.js

    //require express
var express = require('express')
var app = express()

//creating a router object
var router = express.Router()

//export these routes to other files
module.exports = router


//Routes
router.get('/', function (req, res) {
    res.render('index');
})

This is the formHandling folder structure:

This is the formHandling folder structure



Solution 1:[1]

Remove app.use(expresslayouts). The express-ejs-layouts module is used to avoid duplication of code when there are multiple html pages. In the above code there is only one html page, so express-ejs-layouts is not required.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 abilash