Welcome toVigges Developer Community-Open, Learning,Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
764 views
in Technique[技术] by (71.8m points)

node.js - Solved. Ejs Failed to lookup view. Works on localhost but doesnt work online

Solved. I feel stupid. I named file with first letter capital but been giving ejs file without first letter in capital. Idk how and why but its working perfect on localhost but ubuntu server says "Naaa".

I am trying to generate view when user try to access site. View is called "item" and all it does is render data from database. This code works perfect on localhost but doesn't work when I push it to server. Whats funny, I have home.ejs and item.ejs in same dir. Home is beeing rendered perfectly but item not. I even changed item and home code to see if something change and I could render home with item code but couldn't find item with home code.

App.js

// External modules
const express = require('express');
const path = require("path");

// My functions
const {getData} = require("./Database/GetData");

// middleware
const app = express();
app.use(express.static('public'));
app.use(express.json());

// view engine
app.set('view engine', 'ejs');
app.set('views', path.join(__dirname, 'views'));

// Start app
app.listen(4000, console.log("Listening on port 4000"))

// Home
app.get('/', (req, res) => res.render('home'));

// If optional argument id exist, show data from that product, else show all from category
app.get('/koce/:id?', (req, res) => {
    if(req.params.id === undefined){
        getData({koce: 'all'}).then(data => {
            res.render('item',{Data: data, PathName: req.path.slice(1)})
        })
    } else {
        getData({koce: req.params.id}).then(data => {
            res.render('item', {Data: data})
        })
    }
});

item.ejs

<div class="Products">
    <% if (Object.keys(locals.Data).length > 0) {
            if(Object.keys(locals.Data).length > 1){
                    for(let i = 0; i < Object.keys(locals.Data).length; i++) { %>
                        <div class="produkt" onclick='location.href="<%- locals.PathName + "/" + locals.Data[i].id  %>"'>
                            <h3><%- locals.Data[i].id %></h3><br>
                            <img src="<%- locals.Data[i].zdjecie %>" width="90%" height="75%" alt="<%- locals.PathName %>">
                        </div>
                <% }} else {
                        for(let i = 0; i < Object.keys(locals.Data).length; i++) { %>
                                <%- locals.Data[i].id %>
                        <% }
                }%>

    <% } else { %>

        No data

    <% } %>
</div>

App schema

  • Root
    • app.js
    • Public
    • Database
    • Views
      • home.ejs
      • item.ejs

No matter what I try, I always get

Failed to lookup view "item" in views directory "/var/www/html/test/views" at Function.render (/var/www/html/test/node_modules/express/lib/application.js:580:17) at ServerResponse.render (/var/www/html/test/node_modules/express/lib/response.js:1012:7) at /var/www/html/test/app.js:31:17 at processTicksAndRejections (internal/process/task_queues.js:93:5)

Which is the place where I have all my views. I am stuck :/


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
等待大神答复

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to Vigges Developer Community for programmer and developer-Open, Learning and Share
...