Failed to lookup view "/landing.ejs" in views directory - node.js

I'm working in Cloud9 and when trying to run the application i'm greeted with the error failed to lookup view "/landing.ejs" in views directory:
Thus far I have:
var express = require("express");
var app = express();
app.get("/", function(req, res){
res.render("/landing.ejs");
})
app.listen(process.env.PORT, process.env.IP, function(){
console.log("Server is running");
})
My file tree is structed as such App > v1 > app.js package.json views > landing.ejs

At first,you have to set default view files path app.set('views', __dirname + '/view_files_folder_path'); after the line var app = express(); .
Then modify this res.render("/landing.ejs"); to res.render("landing.ejs");
Hope this will work :)

Related

NodeJS, Express, and ejs; why does the test fail, but the view works?

I'm working through a PluralSight course/project on NodeJs and Express. They have a project template with tests. The app is working, it is actually finding and rendering the view. But the test for setting the views directory is failing. I have tried many variations of directories for the views, with and without the '/' after the '/views'; I have tried many variations of using the path.join(__dirname, './src/views') as well. Nothing seems to satisfy the test, even when I tried using the same values in the test, as in app.set('views', path.join(__dirname, '../../src/views')) Nothing seems to satisfy the test.
The tests are run with npm run test:module1. When that runs there is a line in the console: mocha --exit './test/module1/*.spec.js' The test is definitely finding the app because it runs 8 tests; 7 pass and only this one fails.
But I'm trying to re-learn modern JS, as well as Express and ejs, so I may be missing something basic.
Here's my app.js code:
const fs = require('fs');
const path = require('path');
const express = require('express');
const app = express();
app.set('views', './src/views');
app.set('view engine', 'ejs');
app.use(express.static(path.join(__dirname, '/public/')))
app.get('/', function(req, res) {
res.render('index', { title: 'Index'});
})
app.listen(3000, function() {
console.log('PS Project Running on port 3000!');
});
and here's their relevant test:
const path = require('path');
describe('View engine and directory', () => {
it('should set view engine and directory #app-set-views-directory-engine', () => {
assert(typeof app === 'function', '`app` const has not been created in `app.js`.');
assert(app.settings['view engine'] === 'ejs', 'The view engine has not been set to `ejs`.');
assert(app.settings.views === path.join(__dirname, '../../src/views'), 'The view directory has not been set to the `views` directory.');
});
});

Node / Express challenges with shared hosting

I am trying to get a Node app working on A2 Shared hosting. Has anyone got any experience with getting node to work on A2 shared hosting?
My problem seems to be with Express - not even the simplest node test file runs, for example :-
const port = process.env.PORT || 80;
const express = require('express');
const app = express();
app.set('view engine', 'ejs');
app.get('/', function (req, res) {
res.send(`INDEX`);
});
app.get('/about', function (req, res) {
res.send(`ABOUT`);
});
app.listen(port);
console.log(`Listening on port ${port}`);
All I get is :-
Cannot GET /mysite1/
(where mysite1 is the name of the location / application I specified in the Node confirguration in the A2 Hosting control panel.
H

Nodejs ailed to lookup view "home" in views directory "/views"

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
I needed to add the following line to app.js
app.set('views', path.join(__dirname, 'views'));

Why is expressjs returning the error "Cannot GET /" in the browser from code below?

Using the code below with nodejs, expressjs, socket io and pug
the browser response is Cannot GET /
Have had same or similar response to get in other code
Have index.pug file in root and views (just in case)
Tried various permutations such as "/", "./", "/views", "/views" etc
const express = require('express')
const socket = require('socket.io')
const app = express();
app.set('view engine','pug')
app.get("./", function(req, res){
res.render("home");
})
const runServer = app.listen(8585, ()=> console.log('server is running at 1 27.0.0.1:8585'))
Server runs at localhost:8585 in terminal (GIT bash) OK
Expected result: "Home" page in browser
Actual result: the browser when "./" response is Cannot GET /
If using "/" (vs "./") then terminal and browser either crash or respond with blank
Have you got a route for GET / set?
router.route('/').get(function (req, res) {
res.send('app successfully running.')
})
If you are rendering 'home' then why do you have index.pug inside views. You should have home.pug inside views folder. I tried the below code and it worked.
Folder structure:
- server.js
- views/home.pug
Code -
const express = require('express');
const app = express();
app.set('view engine','pug')
app.get("/", function(req, res){
res.render("home");
})
const runServer = app.listen(8585, ()=> console.log('server is running at 1 27.0.0.1:8585'))

Node-Server wrong directory in loading Template via Express

I have some problems when running my Node-Server on Remote via an Service.
I get the following Error:
Error: Failed to lookup view "index" in views directory
"/home/naoufal/etc/run-nodeServer/views" at EventEmitter.render
(/var/www/virtual/naoufal/html/node_modules/express/lib/application.js:579:17)
He looks for my Templatefolder in the path, where the Servicescript is started, namely (/home/naoufal/etc/run-nodeServer/...)
My Node-Application is on (~/html/...).
var express = require('express');
var app = express();
var ECT = require('ect');
var ectRenderer = ECT({watch: true, root: __dirname + '/views',ext: '.ect'});
app.set('view engine','ect');
app.engine('ect',ectRenderer.render);
app.get('/',function(req, res){
res.render('index');
});
app.listen(68000);
I tried instead of ('__dirname + '/views')
This '/home/naoufal/html/views' but the same error occurs...
Any suggestions?
ok I found the solution:
app.get('/',function(req, res){
res.render('index');
});
Here he did not use the right Path, from the Renderer. I don't know why? Instead it takes the path of the executing script...
app.get('/',function(req, res){
res.render(__dirname + '/views/index');
});
solved the problem anyway.

Resources