Node.js express failing to load CSS files

I am currently developing a weather app using node.js and express, but I'm facing an issue where the CSS is not loading. I have followed the instructions provided in this link, however, I have not been successful so far. It's worth mentioning that I have placed the CSS file inside a public folder.

HTML

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

JavaScript

// Setting up local server
const express = require("express");

// Node internal request
const https = require("https");

const app = express();

// Serving CSS files
app.use(express.static(__dirname + "public"));

app.get("/", function (req, res) {

    const query = "Liverpool";
    const apiKey = "3af2a1822cfe6d31e4317ac9c4b5d531";
    const unit = "metric";
    const url = "https://api.openweathermap.org/data/2.5/weather?q=" + query +"&appid="+ apiKey + "&units=" + unit;

    https.get(url, function(response){
        console.log(response.statusCode);

        response.on("data", function(data){

            // Converting hexadecimal to JavaScript
            const weatherData = JSON.parse(data);

            // Extracting desired data
            const temp = weatherData.main.temp;
            const weatherDescription = weatherData.weather[0].description;
            const cityName = weatherData.name;
            const icon = weatherData.weather[0].icon;
            const imgURL = "https://openweathermap.org/img/wn/" + icon + "@2x.png";

            res.write("<p>The current weather is " +weatherDescription + "</p>");
            res.write("<h1>The temperature in " + cityName + " is " + Math.round(temp) + " degrees Celsius</h1>");
            res.write("<img src=" + imgURL + ">");

           res.send();
        })
    });
})

Answer №1

If you encounter any issues, try clearing your cache and performing a hard reload. You can do this by pressing ctrl + shift + i, then right-clicking on the browser's refresh button and selecting the third option.

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

Concerns with uploading images in Angular 2:

Having Trouble with Image Upload in Angular 2: We are facing difficulties uploading images into MySQL as 'blob data' using Angular 2. We are unsure of how to convert the image into blob data. image.html: <input type="file" class="fileinput ...

Oops! The page you're looking for at /api/tasks

Whenever I try to access: http://localhost:3000/api/tasks, I keep getting a "Cannot GET /api/tasks" error message. This is my server.js: var express = require('express'); var path = require('path'); var bodyParser = require('body ...

jQuery functions smoothly on Firefox, but encountering issues on Chrome

While it may seem like a repetitive inquiry, I have thoroughly researched similar questions and have not found a suitable solution. The server is returning the following response through a standard ajax call: <form id="ctl00" name="ctl00" method="post ...

An issue has been detected with the PackageLoader: HTTP driver has not been selected. To make use of the default driver, a selection must be made

While following a tutorial on Udemy and trying to execute using npx ts-node-dev src/main.ts, an error occurred stating [PackageLoader] No driver (HTTP) has been selected. To resolve this issue, I attempted to install the "@nestjs/platform-express" package ...

Two adjacent div elements containing text that seamlessly wraps and shares the same line

Here's a simple challenge for you - How can I make these two divs align on the same line: <div style="width: 200px; padding: 0; background-color: #f00; float: left; display: inline; ">Lorem ipsum dolor sit</div> <div style="margin-left ...

I have chosen Next.js for my frontend development, while our backend developer is crafting the API in Express and MySQL. I am looking for ways to secure and protect the

As a beginner frontend developer learning Next.js, I've been tasked with integrating authentication into our app. The backend developer is working on creating the API using Express and MySQL. After successful login, an accessToken is received. However ...

Struggling to transfer data from componentDidMount as it constantly remains undefined

Having an issue with fetching data from the server in the componentDidMount method and passing it to the handleSubmit function, but it keeps returning undefined: constructor() { super(); this.state = { data: [] }; } componentDidMount() ...

What could be causing the CastError: Cast to ObjectId failed in my code?

Whenever I try to access a specific route in my project, the following error is returned: Server running on PORT: 7000 /user/new CastError: Cast to ObjectId failed for value "favicon.ico" (type string) at path "_id" for model "User" at model.Query.exe ...

Converting a JSON object into an array of objects

I am looking to transform the following JSON structure let data = { item1: [11, 12, 13, 14, 15], item2: [16, 17, 18, 19, 20] } into this specific format using JavaScript's native functionalities of arrays or objects (compatible with all web brow ...

Looking to find top-notch keywords that stand out from the rest?

My chosen keyword is s='young girl jumping' function selfreplace(s) { var words = ['man', 'jumping']; var re = new RegExp('\\b(' + words.join('|') + ')\\b', 'g&a ...

Implementing a JQuery modal with backend coding

I have encountered a problem in my ASP.NET code-behind where I am trying to incorporate a modal popup. Despite my efforts, I have not been able to successfully implement it. Do you have any suggestions on how I should proceed with this process? <scrip ...

`The dilemma of z-index in a dropdown menu nested within an animated card`

Having an issue that I have attempted to simplify in this StackBlitz example (the actual project is created with Angular components and Bootstrap, etc.): https://stackblitz.com/edit/angular-bootstrap-4-starter-njixcw When incorporating a dropdown within ...

Sophisticated sinatra parameter

Creating Dynamic Surveys <script type="text/x-jsrender" id="surveyTemplate"> <li class="question"> <input type="text" name="survey[question]" /> <button class="add_answer">Add Answer</button> <ul> ...

JavaScript can be utilized to generate an array containing duplicated phrases from a given text

Here's a simple concept: you enter text in a textarea, click "send," and receive a list of recurring phrases. When I say phrases, I mean sequences of two or more words that repeat. While I can identify single words, detecting these phrases is where I& ...

My goal is to create a platform where users can upload a basic 3D model and customize it by engraving a rectangular shape onto it using their own specifications

Starting my journey in web development, it seems like I'll be delving into Three.js. However, I'm a bit unsure of where to begin and how to navigate this new territory. The idea is to have an STL file displayed on my webpage with three text fiel ...

What is the most effective method for linking Node.js to SQL Azure?

Struggling to establish a connection between my nodejs app and an Azure SQL Server database. The "msnodesql" module proved to be problematic during installation, with outdated dependencies posing compatibility issues. Is there a superior alternative (mod ...

Determine whether a div contains any child elements

I am working on a piece of code that checks for the presence of the class "wrong" in any of my divs, and if found, displays a jQuery UI dialog box. My goal now is to enhance this code by adding a condition where it also checks for empty divs before showing ...

Exploring and Presenting Arrays using React JS

Recently, I have started working with react js and I am trying to add a search functionality to filter an array in React. My goal is to allow the user to enter a character in the textbox and only see the names that contain that specific character. So far, ...

Is there a way to initiate an AJAX post request with the Authorization header for the initial request using Windows Authentication?

I'm working on a web application that has a video page and a Web API for logging purposes. The events are triggered using an ajax post request: function logAction(actionToLog) { $.ajax({ type: 'POST', url: "/api/v/" + cu ...

Transforming the unmanaged value state of Select into a controlled one by altering the component

I am currently working on creating an edit form to update data from a database based on its ID. Here is the code snippet I have been using: import React, {FormEvent, useEffect, useState} from "react"; import TextField from "@material ...