Ad

Nodejs Ailed To Lookup View "home" In Views Directory "/views"

- 1 answer

I am new to nodejs and I am getting the following error. Error: Failed to lookup view "home" in views directory "/views" I am running the app via a service file. The service file starts the app but I get the above error. However when I start the app using nodejs app.js I do not get the failed error and the app works normally.

app.js file

var express = require('express');
var app = express();
var routes = require('./routes');
var path = require('path');
app.use(express.static(path.join(__dirname, 'public')));
app.set('view engine','ejs');
app.get('/starwars/',routes.home);

routes file.

var moviesJSON = require('../movies.json');
exports.home = function(req,res){
   var movies = moviesJSON.movies;
   res.render('home',
   {
     title:'Star Wars Movies.',
     movies: movies,
     //movies:['Episode 1','Episode 2','Episode 3']
   }
 );
};

I've tried changing the routes location to ../routes but that is giving me a bad gateway error. So I am stumped.

service file

User=webuser
Group=webgroup
ExecStart=/usr/bin/nodejs /home/pi/examples/nodeapp/star_wars_movie_app/app.js
Ad

Answer

I needed to add the following line to app.jsapp.set('views', path.join(__dirname, 'views'));

Ad
source: stackoverflow.com
Ad