Error Express: 'Error: Failed to lookup view "index" in views directory'

Asked

Viewed 18 times

1

I’m trying to set my default folder of the views but something is going wrong because it does not find the views... By the error that returns it seems that he is fetching them in the wrong folder.

Error: Failed to lookup view "index" in views directory "C: Users User Desktop RS Marathon Discover Jobscalc views"

I know it should be right: "C: Users User Desktop RS Discover Jobscalc Marathon src views", but I can’t say why my server.js isn’t adding ' src' before since it’s inside src and I’m using __dirname.

I have tried to put in different formats the line where you arrow the directory but did not succeed. Can anyone tell what is happening?

  • File structure:

Estrutura das pastas

  • The archive server.js where I configure the express:
const express = require('express');
const server = express();
const routes = require("./routes");
const path = require("path");

// Muda o diretório do views
server.set('views', path.join(__dirname, "views"));

// Usar o req.body
server.use(express.urlencoded({extended: true }));

// Setar a view engine
server.set('view engine', 'ejs');

// Trazer CSS e JS - estaticos
server.use(express.static("public"));

// routes
server.use(routes);

server.listen(3000, () => console.log(">> Servidor rodando na porta 3000..."))
  • A smaller example of how I’m using the routes (in another file):
const express = require('express');
const routes = express();

routes.get('/', (req, res) => res.render("profile"));
  • Entire error:
Error: Failed to lookup view "profile" in views directory "C:\Users\User1\Desktop\RS\Maratona Discover\JobsCalc\views"
    at Function.render (C:\Users\User1\Desktop\RS\Maratona Discover\JobsCalc\node_modules\express\lib\application.js:580:17)
    at ServerResponse.render (C:\Users\User1\Desktop\RS\Maratona Discover\JobsCalc\node_modules\express\lib\response.js:1012:7)
    at C:\Users\User1\Desktop\RS\Maratona Discover\JobsCalc\src\routes.js:179:35
    at Layer.handle [as handle_request] (C:\Users\User1\Desktop\RS\Maratona Discover\JobsCalc\node_modules\express\lib\router\layer.js:95:5)
    at next (C:\Users\User1\Desktop\RS\Maratona Discover\JobsCalc\node_modules\express\lib\router\route.js:137:13)
    at Route.dispatch (C:\Users\User1\Desktop\RS\Maratona Discover\JobsCalc\node_modules\express\lib\router\route.js:112:3)
    at Layer.handle [as handle_request] (C:\Users\User1\Desktop\RS\Maratona Discover\JobsCalc\node_modules\express\lib\router\layer.js:95:5)
    at C:\Users\User1\Desktop\RS\Maratona Discover\JobsCalc\node_modules\express\lib\router\index.js:281:22
    at Function.process_params (C:\Users\User1\Desktop\RS\Maratona Discover\JobsCalc\node_modules\express\lib\router\index.js:335:12)
    at next (C:\Users\User1\Desktop\RS\Maratona Discover\JobsCalc\node_modules\express\lib\router\index.js:275:10)
No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.