Muhammad Ravat
Muhammad Ravat

Reputation: 7

I keep getting a 404 when rendering css folders on railway

Whenever I try to host my express app on railway it gives a 404 when i try to get my CSS files. This is my file structure:

Public - styles.css, output.css Views (index.ejs etc) - Partials (head.ejs):

<head>
    <link rel="stylesheet" href="https://fonts.googleapis.com/css2?family=Material+Symbols+Outlined:opsz,wght,FILL,GRAD@40,400,0,0" />
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <script src="https://kit.fontawesome.com/b99e675b6e.js"></script>
    <script src="https://kit.fontawesome.com/71da1e9480.js" crossorigin="anonymous"></script>
    <link rel="stylesheet" href="/styles.css">
    <link rel="stylesheet" href="/output.css"> 
    <title>Title</title>
</head>

app.js:

const express = require('express');
const morgan = require('morgan');
const mongoose = require('mongoose');
require('dotenv').config();
const path = require('path');

const app = express();
const dbURI = process.env.DB_LINK;
mongoose.connect(dbURI)
  .then(result => {
    const port = process.env.PORT || 3000;
    app.listen(port, () => console.log(`Server is running on port ${port}`));
  })
  .catch(err => console.log(err));


  app.set('view engine', 'ejs');
  app.set('views', path.join(__dirname, 'Views'));
  
app.use(express.json());

app.use(express.static(path.join(__dirname, 'public')));
app.use(express.urlencoded({ extended: true }));
app.use(morgan('dev'));
app.use(cookieParser());
app.use((req, res, next) => {
  res.locals.path = req.path;
  next();
});
app.get('/', (req, res) => {
  res.render("index", {title: "Home Page"});
});

I also tried using /public/styles.css but that didn't work, though my ejs files do render so I don't understand the problem.

Upvotes: 0

Views: 9

Answers (0)

Related Questions