最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

node.js - railway.com cannot find index.ejs although it exists - Stack Overflow

programmeradmin1浏览0评论

I'm trying to get a url for my local host website using node.js and railway. the code works fine in my local host. but when railway generates the website URL i receive this Error: Failed to lookup view "index" in views directory "/app/views". I attached a picture for my folder structure. [![Folder Structure

import express from "express";
import axios from "axios";
import bodyParser from "body-parser";
import path from "path";
import { dirname } from 'path';
import { fileURLToPath } from 'url';

// Simulate __dirname
const __dirname = dirname(fileURLToPath(import.meta.url));
const app = express();
const PORT = process.env.PORT || 3000;


// Set the correct views directory
app.set("views", path.join(__dirname, "views"));
app.set("view engine", "ejs");

let data;
let result;
//app.use(express.static('public'));
app.use(express.static(path.join(__dirname, "public")));
app.use(bodyParser.urlencoded({ extended: true }));

app.get("/portofolio", (req,res)=>{
  res.redirect("///");
})

app.get("/", async (req, res) => 
{
    try 
    {
      res.render("index",{DrinkType: data});
    } 
    catch (error) 
    {
    console.log(error.response.data);
    }
});
app.get("/home", async (req, res) => 
{
    try 
    {
      data=null;
      res.render("index",{DrinkType: data});
    } 
    catch (error) 
    {
    console.log(error.response.data);
    }
});
app.post("/cocktail", async (req, res) => 
{
    try 
    {
      result = await axios.get(`.php?s=${req.body.choice}`);
      data=result.data.drinks[Math.floor(Math.random()*result.data.drinks.length)]
      
      res.redirect("/");
    } 
    catch (error) 
    {
    console.log(error.response.data);
    }
      });

app.listen(PORT, () => {
    console.log(`Server is running on port ${PORT}`);
  });
发布评论

评论列表(0)

  1. 暂无评论