Express: Error: Failed to lookup view "./index" in views directory "/app/templates/views"

Created on 9 May 2019  路  2Comments  路  Source: expressjs/express

my app is working fine in local machine but when i am deploying it to production it give 'internal server error' on the website

src/app.js -->

const path = require('path');
const express = require('express');
const hbs = require('hbs');

// const require = require('require');
const geocode = require('./util/geocode');
const forecast = require('./util/forecast');


const app = express();

const port = process.env.PORT || 3000

console.log("xxxxxxxxxxxxxxxxxx",port)

app.listen(port);


//using public directory to load static path
const publicPath = path.join(__dirname,'../public');
app.use(express.static(publicPath));

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

//     res.send('Hello Express')
// })

app.get('/weather', (req,res)=>{

if(!req.query.address){
   return res.send({
        error:'Please provide different address'
    })
}
geocode(req.query.address,
        (error,data)=>{
            if(error){
               return res.send({
                error:'Please provide different address'

               })

            }
            geoData = data;
            forecast(data.latitude, data.longitude, 

                (error,data)=>{
                    if(error){
                        return res.send({
                            error:'Please provide different address'

                           })


                    }
                    res.send(
                       {[ req.query.address]:{
                            searchedlocation: req.query.address,
                            location: geoData.location,
                             weather: data
                        }
}


                    )


                    }

                )



            }
        );






})


//using view directory of templates
const viewsPath = path.join(__dirname,'../templates/views');
app.set('view engine', 'hbs')
app.set('views', viewsPath)

app.get('/about', (req,res)=>{
    res.render('./about')
})

app.get('/', (req,res)=>{
    res.render('./index')
})

app.get('/help', (req,res)=>{
    res.render('./help')
})






//using partials directory of templates directory for dynamic view

const partialPath = path.join(__dirname,'../templates/partials')
hbs.registerPartials(partialPath);

question

All 2 comments

Hi @mayank13395, that error typically means the file is inaccessible. Could be permissions or could be missing. I would check those. If you have any other questions like this, a better place to reach out is Stack Overflow or the Express Gitter. Good luck!

Hi

Can you please tell me ....on how did you solved the above error, please
Even I'm facing the same issue while deploying my app in Heroku

Was this page helpful?
0 / 5 - 0 ratings

Related issues

THCloud picture THCloud  路  23Comments

yahao87 picture yahao87  路  27Comments

sgentle picture sgentle  路  32Comments

lykkin picture lykkin  路  20Comments

adamterlson picture adamterlson  路  24Comments