Having trouble loading the linked CSS file in the Jade template

My directory structure is organized as follows:

--votingApp
    app.js
    node_modules
    public
      css
        mystyle.css
    views
      test.jade
      mixins.jade

In the file mixins.jade, I have created some general purpose blocks like 'boilerplate', 'nav', and 'nav item'. The main file is test.jade.

include mixins
doctype html 
html(lang="en")
    head
        +boilerplate
        link(rel="stylesheet",type="text/css",href="../public/css/mystyle.css")
    body
        +nav("Voting app","navigation_menu")
            +nav_item("#","active") Home
            +nav_item("#") Signup
            +nav_item("#") Login

Here is my app.js file:

var express=require('express');
var path=require('path');
var app=express();
app.use(express.static(path.join(__dirname,'public')));
app.set('views','./views');
app.set('view engine','jade');
app.get('/',function(req,res){
    res.render('test.jade');
});
app.listen(8000);

The issue I am facing is that the mystyle.css file is not being loaded. When checking the network option in the developer console of Mozilla, it shows error 404 for the request for mystyle.css (request url-http://localhost:8000/public/css/mystyle.css) Can someone please advise me on what to do? Thank you for your assistance.

Answer №1

You can skip using the relative path for loading your css file, as you have already designated your public directory as a static resource.

To update your link line, use the following:

link(rel="stylesheet",type="text/css",href="/public/css/mystyle.css")

Answer №2

All you need to do is:

link(rel="stylesheet",type="text/css",href="/css/mystyle.css")

Answer №3

Absolute path is the way to go with node.js, as relative paths won't work properly. To ensure everything runs smoothly, make sure to specify absolute paths like this:

link(rel="stylesheet",type="text/css",href="css/mystyle.css")

Additionally, you should designate your directory as a public folder by including the following line in your code:

app.use(express.static(path.join(__dirname,'public')));

This means that there's no need to include "public" in the path when referencing CSS files.

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

Array in Javascript reset to null after its second iteration

''' const users = [] const addUser = ({ id, username, room }) => { // Clean the data username = username.trim().toLowerCase() room = room.trim().toLowerCase() // Validate the data if (!username || !room) { r ...

When will the search bar toggle and the logo appear sliding down?

.navbar { background-color: #F91F46; } .src-bar { border: 0; border-radius: 5px; outline: none; padding-left: 15px; width: 30vw; } <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/bootstrap/5.3.2/css/bootstrap.min.css" in ...

Struggling to display a function's output on a separate web page within a flask application

After spending close to 10 hours on this, I find myself stuck. My restaurant search app is functioning perfectly when I print the output to the terminal. However, I can't seem to figure out how to display it on a new page. Here's a snippet of my ...

"Once the initial date has been selected, v-Calendar's datepicker allows for setting a

Is there a way to trigger an event for the date range picker of v-calendar after the first date is picked or prevent the inputs from adding the dates until both dates have been selected? Here is the Vue component I have: new Vue({ el: "#app", data( ...

Currently, I am encountering challenges with the navigation menu on my website. The technologies I am utilizing for this project include Bootstrap 3, JQuery 2.3.1, Jade, ExpressJS

Greetings! I am diving into the world of Jade templating for Node.js and embarking on a journey to create a basic starter application using Express 4, JQuery, Bootstrap 3, and Jade. I encountered an issue while attempting to incorporate the navbar into my ...

The FormData object appears to be blank, even though it was supposed to be populated when attempting to send a PDF file using a multipart FormData POST request in Cypress

I am attempting to send a PDF file as a POST request. The API supports the use of @RequestPart and @RequestParam: @RequestPart("file") MultipartFile file; @RequestParam(value = "document-types", required = false) Set<String> documentTypes; My appro ...

Mongoose fails to carry out internal query using Promise mechanism

Just diving into the asynchronous and await world, I decided to play around with Mongoose + MongoDB + Node.JS. Here is a snippet of my code: exports.updateBrandPreferences = async (req,res) => { var userID = req.body.playerID; var newBrands = r ...

What is the best method for saving the graphql response from "apollo-fetch" into a variable?

In order to enhance the efficiency of my code, I set out to extract the title, forename, and surname from a GraphQL response for an automated test. The initial implementation involved making separate HTTP requests for each value as shown below: const { cr ...

Dynamic Label Editing on ASP Page Using Master Page

I am working on an ASP page with a Masterpage element on my website. The specific functionality I am trying to implement involves having multiple labels with an edit hyperlink next to each one. When the user clicks on the Edit hyperlink, I want the label t ...

Is there a way to automatically remove files from the "dist" directory when deleting the prototype from the "src" folder?

I am new to build systems and seeking assistance with the following question. Here is the structure of my boilerplate: /src (working folder) ___/templates(jade files) ___/scss ___/scripts /dist (compiled files) ___/css ___/js ___.html files In the te ...

Guide on using multer to retrieve files from digital ocean spaces

I successfully uploaded PDF files to Digital Ocean Spaces using a Node.js app. However, I am struggling with accessing these files and displaying them to the user. The code snippet below is from a tutorial on object storage file upload, but it does not pro ...

Encountering the "TypeError: document.getElementById(...) is null" error message while utilizing react.js in conjunction with chart.js

I am encountering an issue while using react and chart.js together to create a bar chart. The problem lies in the fact that chart.js requires the use of canvas tags, and we need to locate this tag and insert the bar chart within it using the traditional do ...

Having Trouble with Sending Emails Using Google Scripts - Javascript POST Request Issue

I have been working on setting up a basic form on my website where users can input their name, email, and a short message. This information is then sent to Google Apps Script which forwards the message to me via email. Unfortunately, I keep encountering an ...

The struggle between Node.js 404 errors and Angular's URL refresh issue

I am currently developing a Node.js and AngularJS application. I encountered an issue where a page loads successfully when the URL is entered, but then I added a script to redirect to a 404 error page. Now, only one of the criteria works at a time - either ...

Encountering an error during the registration process of @fastify/middie

I am currently in the process of integrating Fastify into a small project I am working on. One of the key requirements for this project is the utilization of Middleware (via @fastify/middie). However, when I follow the necessary steps to register the middi ...

Guide to utilizing the Array find method to retrieve an object in JavaScript

I'm currently working on a Homework assignment that involves passing a parameter called userID to an arrow function. Within this function, the task is to use the .find method on an array named users. This find function should retrieve an object contai ...

Wireless web platform/program

I have been tasked with creating an app that will display real-time power data from a website. The challenge is that I do not have access to the backend of the site, which means I am unable to use PHP protocols to pull the data into my app. I attempted to ...

Programmatically adjusting the color of mask images - Creative image theming

Is it possible to alter the color of an image hosted on a different website? Here is a link to an example image: Is there a technique to overlay a specific color on the image and only modify certain colors, such as changing or adding a layer of light gre ...

What is the best way to block a user from joining a room in socket.io?

Currently, I am working on creating an online chat room using express4 and socket.io version 0.9. One issue I am facing is preventing the same user from joining a room multiple times when they open another tab in the browser (or use a different browser). ...

What is the best way to manage URL data between a domain and hashtag using Vue Router?

I am currently developing a project using vue-cli, vue router, and node.js. The routing in Vue is set to hash mode so that users can easily navigate to specific pages by copying and pasting URLs or typing from memory without encountering any errors. Howeve ...