3

I'm building a simple website using Node.js, Express and the EJS template engine and I'm having trouble getting my external CSS files to load on the routes that have route parameters. My code looks like this:

const express = require("express");
const app = express();
var AWS = require('aws-sdk');
var uuid = require('uuid');
var request = require('request');
var Amplify = require('aws-amplify');
const { info } = require("console");

app.use(express.static("public"));
app.set("view engine", "ejs");

app.get("/Test1/", function(req, res){
res.render("Test1.ejs");
});

app.get("/Test2/:route", function(req, res){
res.render("Test2.ejs")
});

Test1.ejs and Test2.ejs are exact duplicates of each other, one being copy pasted from the other.

I'm able to to see the CSS file loading properly on Test1 but on Test2 express is sending the HTML of Test2.ejs in place of the actual CSS file. This is without reloading or changing anything. I would be less confused if the CSS just wasn't loading properly on any of my routes, as that would indicate some problem with how I'm using express, but I can't figure out how it can be working properly on all my other routes and fail whenever a route parameter is used. I'm also tried actually using the route parameter e.g.

app.get("/Test2/:route", function(req, res){
const routeParam = req.params.route;
res.render("Test2.ejs", {
    routeParam: routeParam
});

});`

I also displayed routeParam in the page to make sure that was working, and it does, except the page is still getting the HTML of the page in place of the proper CSS file, causing it to be poorly formatted.

I'd appreciate any help on this, thank you

2 Answers 2

3

Thanks for looking into this CoderFF, you helped me figure out how to get it to work. It look like the problem was that my css file was directly inside the public folder, as opposed to in a subdirectory within it. The html has to be like this:

<link rel="stylesheet" type="text/css" href="/css/customStyle.css" />

it has to include that leading '/' in the href tag. I don't know how it was working for me before without that on routes without route parameters, but it was. The same seems to apply for images in an /images/ folder within the public folder.

Sign up to request clarification or add additional context in comments.

Comments

2

I can't really reproduce this. What I'm doing is:

  1. Placed my CSS in public/css/all.css. File contains only one line: html {background-color: #aaaaaa} so I can tell it's loaded
  2. Place two templates into views/Test1.ejs and views/Test2.ejs and put the same contents in them: <html><head><link rel="stylesheet" type="text/css" href="/css/all.css"/></head><body></body></html>

And it works perfectly.

I suppose that you've created a folder Test2 inside of your public/ folder, in which you placed your CSS. In that case, your path to the CSS is /Test2/some.css matches your second route and instead of getting a CSS file, you get an HTML from the route.

In that case, moving your CSS files will help.

2 Comments

Thanks for looking into it, but I'm still having this issue. Test1.ejs and Test2.ejs are already pulling from the same CSS file (customStyle.css in the 'public' folder). I recreated the issue in a new project, as well. I can't figure out what's wrong, the css line in both test1.ejs and test2.ejs looks like <link rel="stylesheet" type="text/css" href="customStyle.css" />
I've pushed my code to the GitHub. Try checking it out: github.com/CoderFF/express_css_help . Don't forget to run npm install

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.