The individual blog pages on my site are not receiving the static files they need to display

I'm facing an issue serving a css file to individual blog posts on my website's blog section.

Here's how it's supposed to work: Visit /blog- you'll see the main blog page, which is functioning fine.

However, when trying to access, for instance, /blog/post1, I encounter this error:

http://localhost:4000/blog/static/css/style.css

I could really use some assistance since I'm fairly new to express and routing files. Thanks in advance.

My folder structure is as follows


    blog
        /node_modules
        /src
            /mock
            /public
                /css
                    style.css
            /templates
                /partials
                    _head.jade
                    _nav.jade
                blog.jade
                index.jade
                layout.jade
                post.jade
            app.js

Here's how it should work: Go to /blog- you get the blog page and everything is good.

But when trying to navigate to, say, /blog/post1, I encounter the error

http://localhost:4000/blog/static/css/style.css

This is what my respective files look like, maybe there's something missing:

app.js

"use strict";

var express = require("express"),
    posts = require("./mock/posts.json");

var postsLists = Object.keys(posts).map(function(value){
    return posts[value]
    });

var app = express();

app.use("/static", express.static(__dirname + "/public"))

app.set("view engine", "jade");
app.set("views", __dirname + "/templates");

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

app.get("/blog/:title?", function(req, res){
    var title = req.params.title;
    if (title === undefined) {
        res.status(503);
        res.render("blog", {posts: postsLists});
    } else {
    var post = posts[title] || {};
    res.render("post", {post: post} );
    }
});

app.get("/about", function(req, res){
    res.send("<h1>About Page</h1>");
})

app.get("/projects", function(req, res){
    res.send("<h1>Projects Page</h1>")
})

app.listen(4000, function(){
    console.log("Frontend server is running on port 4000.")
});

_head.jade

head
meta(charset="UTF-8")
link(rel="stylesheet", href="static/css/style.css")

layout.jade

doctype html
html(lang="en")
include ./partials/_head.jade
body
block content

blog.jade

extends ./layout

block content
    section(id="postHolder")
        for post in posts
            div.post
                h2 #{post.title}
                p #{post.body}
                a(href="/blog/" + post.title)
                    button Read More

post.jade

extends ./layout.jade

block content
    section
            div.post
                h2 #{post.title}
                p #{post.body}
                p This is the actual post page itself.

Answer №1

Perhaps following these steps will lead you to success -

start
  label(lang="en")
  link(type="text/css", href="/styles/custom.css")

Answer №2

Let me point out something in my _head.jade file that needs your attention:

head
meta(charset="UTF-8")
link(rel="stylesheet", href="/static/css/style.css")

I had to update the reference to make it an absolute path by adding a "/" in front of "static"

Originally, it was static/css/style.css and now it's /static/css/style.css. I'm still learning, so I hope I clarified the concept of an absolute path correctly.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

What could be causing my MongoDB Node.js driver's $lookup aggregation query to not produce the expected results?

In my database, I have two collections: users and roles Each user has an array of roles assigned to them: { _id : 61582a79fd033339c73ee290 roles:[615825966bc7d715021ce8fc, 615825966bc7d715021ce8fd] } The roles are defined as follows: [ { ...

Javascript dynamically generates CSS animations

I need assistance creating a div with a CSS3 animated timer inside it when a button is clicked. Here is the code I have been working on: http://jsfiddle.net/arcco96/y5nov6rz/1/. Unfortunately, the div is not being created as expected. I believe the code sh ...

Tips for creating a custom Menu with content overflow in a single direction

What is the goal of this task? I have developed a custom Menu in my application with 8 options. There is a need to dynamically disable certain menu options based on specific conditions (e.g. disabling the last 4 options). When a user hovers over a disable ...

Troubleshooting the unresponsive controller callback function within the Angular-Express-Bootstrap Seed configuration

I recently downloaded the Angular-Express-Bootstrap seed from https://github.com/jimakker/angular-express-bootstrap-seed and have been working on setting up routing through Angular JS. While the routing is working perfectly, I am now encountering an issue ...

Unable to showcase the compilation in PDF form

I have a link on my page that, when clicked by the user, retrieves a list from the database using an ajax call and displays it. Now, I'm looking to add another link that, when clicked, will fetch the list from the database via ajax and present it in ...

Changing the color of Material UI pagination: a step-by-step guide

I have integrated Material UI to create a pagination bar for my website. While everything is functioning properly, I am looking to personalize the appearance. After reviewing their documentation, I tried implementing a theme but encountered an issue with c ...

Dynamic placement of divs when new content is added to the page

Before we begin, I'll provide you with the complete HTML code of my webpage: <div align="center"> <img src="http://questers.x10.bz/Header.png" style="position: absolute; margin-left: -440px; box-shadow: 0px 3px 12px 2px #000;" class="rotate" ...

Query a document in Mongoose to check if an element is not present in an array

I am currently facing an issue with querying a notification schema receiver: [{ type: mongoose.Schema.Types.ObjectId, ref: 'Profile' }], readBy: [{ readerId: { type: mongoose.Schema.Types.ObjectId, ref: 'Profile', d ...

Tips for resolving Cypress run warnings using personalized Windows Powershell parameters?

I have a Powershell script that I need to modify It is crucial for me to pass arguments as I am using Uipath to trigger Cypress Param( $RunType="--headed", $ProductCode="WR", $TestFile="cypress/integration/dongle/1-purchase.js" ) cd C:\ ...

Utilize the Winston Node logger to save JSON logs with date and time formatting

Currently, I am attempting to use winston to log json data while including a timestamp. Below is my current configuration: 'use strict'; import * as winston from 'winston'; const LOG = !!process.env.LOG; export const { error, info, ...

Troubleshooting problem when creating a customized PDF name using pdfmake and Node.js

I have been using pdfmake to generate PDFs in Node.js and then sending the data to my Angular request page. The PDF generation works fine, but I am facing an issue with the file name when downloading it - it shows up as "16064905-c4aa-4d40-96db-ca7464c3885 ...

Executing Python code within a Node.js application

Question: How can I execute my python script within my node application? Successful method: I have been able to run the following command in the command line successfully. python generatePersonTerraform.py -s http://localhost:8080/api/person/239/export ...

transferring information through POST requests in Angular and managing the data in Node.js

Recently, I was experimenting with a post request in Angular and encountered some difficulties sending data to the back-end server using Node.js. It seems like either I am not handling the data correctly on the server side or my data is not being sent in t ...

Unable to locate / within the Node application

I've searched through the related questions but can't seem to figure out what's causing the issue (I'm not very experienced in this area). I am attempting to run this app locally with its default settings (except for some Twilio keys), ...

Invoking an AWS Lambda function from a separate Lambda function with node.js

I'm encountering an issue where I am attempting to invoke an AWS Lambda function from another AWS Lambda function, but for some reason the second lambda function is not being called. Below are the details of the two lambda functions. The first Lambda ...

What is the best way to create a unique background shape for Bootstrap 4 carousel controls?

Hello, I am currently working on a Bootstrap 4 Carousel where my controls are replaced by arrow images. I am attempting to create an oval-shaped background for the arrows as shown in this picture. However, I am facing issues with adjusting the border to ...

Restoring gitignore files: A step-by-step guide

After downloading a zipped file from github, I realized that some features were not working due to a gitignore in the file. Is there a way to extract data from the gitignore file in order to make all features of this app function properly? It seems like t ...

Utilizing dynamic, MongoDB-inspired expressions to eliminate specific elements from an array

In the process of creating a lightweight database similar to MongoDB, I am incorporating an object-oriented query language where references can be functions or object references: foo.users.find( args ); foo.users.remove( args ); The 'args' para ...

Upgrading GKE results in terminating Pods that have ongoing requests

Our team has been facing challenges with the Google Kubernetes Engine (GKE) as regular upgrades to newer versions are causing disruptions in our cluster's pods and containers. While we understand the importance of updates, the issue arises when the cl ...

Styling the TinyMCE Toolbar within a Div container

While working on creating a unified TinyMCE toolbar that is fixed at the top of multiple containers within a div wrapper, I encountered an issue where the CSS styling of the toolbar was lost after wrapping. The toolbar only displayed hyperlink URLs inste ...