What is the best way to import a css file from a static directory?

I am struggling to load a CSS file in the header of my Handlebars (HSB) template for home. My backend is built using Node.js with Express. I suspect that I may have set the path incorrectly, but I'm unable to pinpoint the exact issue.

Could someone please point out what I might be doing wrong?

Here is my folder structure:

public
  -> home.css
  -> login.css
  -> signup.css

src
  -> index.js

templates
  -> home.hbs
  -> login.hbs
  -> signup.hbs
  -> user.hbs

index.js:

...

home.hbs file:

...

I suspect that my path to the static folder "public" is incorrect, but I can't figure out why?

Answer №1

Make sure to include the leading slash before home.css in your href

<link rel="stylesheet" href="/home.css">

The use of a leading slash / indicates that the path should start from the root of your application. This lets the browser search for the home.css file directly within the public directory.

Agreeing with @76484 about using path.join

You can implement it like this

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

For ensuring accurate paths when serving static files with Express, it is recommended to utilize the path.join() method to concatenate directory paths. By incorporating path.join(), you can avoid potential issues related to path concatenation and maintain consistency in serving static files across different operating systems.

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

I am utilizing React to pull data from the database with a one-day discrepancy in the backend

Despite trying numerous solutions, I am still puzzled as to why this issue persists. For instance, in my database, there is a date listed under the "date_of_birth" column as 2023-12-29. However, when examining my backend code, it displays as "date_of_birth ...

HTMLKit is functioning properly on the simulator but is experiencing difficulties when running on the

Currently, I'm utilizing HTMLKit as a dependency in my project. It runs smoothly on the simulator but encounters issues when running on an actual device. Below is the crash log generated while attempting to run it on an iPhone: dyld: Library not ...

Creating trails by following the cursor's movement in KineticJS

Currently, I am working on a drawing application using KineticJS. While I have successfully used it to draw shapes and straight lines by following the method outlined in KineticJS - Drawing Lines with Mouse, I now need to draw a line along the mouse's ...

Search for records in MySQL using Typeorm with the condition "column like %var%" to retrieve results containing the specified

Looking for a way to search in MySql using typeorm with the "column like" functionality. async findAll({ page, count, ...where }: CategorySelectFilter): Promise<Category[]> { return this.categoryRepository.find({ where, ...

Guidelines for managing a catch function

I have recently set up a database in Cloud Firestore and am looking to populate it with information using input fields. However, I've encountered an issue where if the input fields are left empty, the information still gets stored and the catch functi ...

What are the ways to prevent the slider from automatically moving?

I have a carousel/slider that is automatically moving, but I want to stop it so that it only moves when the navigation arrows are clicked. Here is a picture of the slider https://ibb.co/HPr9rJR. Currently, it is moving on its own to the left side, but I wo ...

What is the best way to retrieve the values of "qty" and "price" and access them within the item [array] without knowing the IDs?

I am currently working on a shopping cart project, specifically on the "previous orders" page to display order details. My goal is to output this information in an (.ejs) file. The data structure includes nested objects, and within one object, propertie ...

A step-by-step guide on setting up the localtunnel and SendGrid integration test webhook

After constructing a simple express app, I decided to use localtunnel for webhook testing purposes. However, my efforts to set up the sendgrid Event notification webhook have been unsuccessful as the data doesn't seem to be reaching the POST route dur ...

Error in Socket.IO: The object does not have the method "packet"

I encountered an issue with global event emission using Socket.IO. This is a server-side error, not related to #489. Situations where the error occurs: nsp = io.of('/namespace'); nsp.emit('hello', 'there'); io.emit('h ...

In CSS, when text within a tab div lacks spaces, it triggers a horizontal scrolling effect

My special css style for the div: .message-body { background: #5181a2; padding: 8px 5px; text-align: justify; } I aim to display this particular text inside the div similar to how 'hello hello' appears on the line above. ...

Generating a personalized array by matching an array with an object using JavaScript / Node.js

let obj ={ 97:{ data:"OS", location:"system", access:"globally", parameters:"aed31" }, 490:{ data:"Ae-ds", location:"rootpath", access:"admin rights", parameters:"4fsje" }, 278:{ data:"config", location:"system", ...

SVG Mask not functioning properly with SVGs created in Illustrator (in all web browsers)

Alright, let's get to it - I've got a couple of SVGs here. The first one is a blue circle and the second one is a map of the United States. (Both of these were created in Illustrator) Here's what I want to do: I want the map to serve as a ...

Error message: The desired resource at / is not accessible using NodeJS with Express

I am encountering an issue where the file "default.htm" exists and successfully loads when using the readFile function in NodeJS. var express = require('express'); var app = express(); app.use(express.static(__dirname + '/default.htm' ...

Invoking Node to utilize React/Webpack module code

Trying to figure out how to integrate client-side import/export modules into a Node.js require script within my custom NextJS webpack config: module.exports = { webpack: (config, options) => { if (options.isServer) { require("./some-scr ...

Is there a way to implement hover behavior for a Material-UI Button within a ButtonGroup component?

When using MUI v5, I am encountering an issue where the first button in the code provided is only half working. The button is initially colored red (both the border and text), however, upon hovering over it, the color of the border changes to blue. This is ...

Navigate the contents within a DIV using scrolling

Is there a way to display items in a scrollable format, but only show them when the scroll reaches the bottom of a specific parent div (#left-cnt) instead of the window? <div id="left-cnt" class="col-md-8"> <div class="elements scroll">El ...

"Ionic start" command abruptly stops with the message "Unable to start project within an existing project" despite the folder being empty (Ionic 5 / Node 12)

I recently set up an empty folder that I plan to use for a basic starter project with Ionic (based on Angular). Here's what happened when I tried to start the project: ionic start "My App" blank --no-confirm: You are currently in an existing Ioni ...

Node.js's splice method experiences decreased performance when handling over 70,000 items

As a beginner in node.js, I attempted to add 70000 items to an array and then delete them all: var Stopwatch = require("node-stopwatch").Stopwatch; var stopwatch = Stopwatch.create(); var a = [] stopwatch.start(); for (var i = 1 ; i < 70000 ; i++) ...

Node.js Error: Attempting to access property 'push' on an undefined object

Hello there! I'm currently learning about nodejs and mongodb, and I'm working on a social media project where users can push comments on posts. However, I encountered an error in my controller while trying to push comments into mongodb. TypeError ...

Building Icon-Filled Columns for Contact Details in Bootstrap List Groups

I have a project that closely resembles the code snippet below, with the addition of Material Design icons as outlined in . <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" integrity="sha384-Gn5384xq ...