What is the best method to deliver CSS/JS using Crow C++?

When attempting to serve a load.css file alongside index.html, I am encountering a 404 error. I experimented with manually setting the res.body == load.css data, but this caused the request to remain stuck on loading.

What could be causing this issue?

void foo() {
    crow::SimpleApp app;
    crow::mustache::set_global_base("/home/cereza/testing/src/frontend/src/");

    CROW_ROUTE(app, "/") ([] () {
        auto page = crow::mustache::load("html/index.html");
        return page.render();
    });

    CROW_ROUTE(app, "/style/load.css") ([] () {
        crow::response res;
        res.set_static_file_info("style/load.css");
        return res;
    });
}

I have already attempted adjusting paths and verifying if files are being correctly retrieved.

Answer №1

There is no need to create a separate route for serving JS and CSS files. These files are considered "static" and should be placed in a designated folder named "static".

Your project directory structure should resemble the following:

└── mainRoot/
    ├── static/
    │   ├── script1.js
    │   ├── script2.js
    │   ├── style1.css
    │   └── style2.css
    ├── templates/
    │   ├── home.html
    │   └── about.html
    ├── server.cpp
    └── additionalFile.cpp

You can eliminate the unnecessary route from your server configuration:

int main() {
    crow::SimpleApp app;
    crow::mustache::set_global_base("/path/to/project/src/frontend/src/");

    CROW_ROUTE(app, "/") ([] () {
        auto page = crow::mustache::load("home.html");
        return page.render();
    });
}

In your home.html file, you can reference the JavaScript and CSS files as usual:

<link rel="stylesheet" href="/static/style1.css">
<script src="/static/script1.js"></script>

For more information, refer to the static files section in the Crow documentation.

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

Utilizing React Bootstrap with TypeScript for Styling Active NavItem with Inline CSS

Is it possible to change the background color of the active NavItem element to green using inline CSS in React Bootstrap and React Router Dom? I am currently using TypeScript 2.2 and React. If not, should I create a CSS class instead? Here is the code sni ...

Displaying article title above container with backdrop picture

Currently, I am working on a specific layout design using Bootstrap 5 where I am trying to render a tag outside its original container so that it takes up the full width of the page. My attempts with absolute positioning on the img tag have proven to be ch ...

Troubleshooting CSS Animation Failure in Internet Explorer 11

My mouse over / mouse out animation works perfectly in Firefox and Chrome, but it's not functioning in IE. Can anyone suggest why this might be happening when it was working fine before? var actual = 1; var over = 0; var over2 = 0; function scrol ...

When toggling the menu in bootstrap, the functionality of <a href=""> links may be disrupted

My attempt to integrate a toggle function into each link in the menu to automatically close the menu on mobile after selecting a link has hit a snag. With the solution of adding data-toggle="collapse" data-target="#navbarSupportedContent" to the <a href ...

nsIStyleSheetService - The Gateway to Stylish CSS

I am currently utilizing nsIStyleSheetService within my Firefox extension to apply a custom stylesheet to every webpage. While everything is functioning properly, there are a few styles that are not being applied as expected. One of the styles causing iss ...

Obtaining the IP address of the interface with a route to the specified host

Looking to develop a simple message queue library that will facilitate point-to-point messaging service between two servers through TCP or UDP. The goal is to eliminate the client/server distinction since both servers will have the same function of sending ...

jQuery: Select the parent element of a focused form input and apply styling

Recently, I encountered a situation where I have a form containing a DIV along with 3 INPUTS, each enclosed within a LABEL element. My goal is to alter the background image of the DIV whenever an INPUT receives focus. Due to limitations in navigating up t ...

When using the Android platform, the webpage may not be loading correctly and certain links may be unresponsive in the webview

While viewing my webpage on Google Chrome in my Android phone, the website loads perfectly and functions as expected. However, when I try to load the same webpage in Webview, the site does not load correctly, especially the jQuery content. Additionally, no ...

Compiling perl for Android using cross-compilation encountered an issue: ld.lld reported an error stating that it was unable to

Hello, Currently, I am attempting to cross compile perl for Android by following a tutorial that can be found at Compiling Perl for Android. However, I have encountered a roadblock while running the Configure Script in the Perl directory: Checking your ch ...

The rotation feature for legends in Highcharts does not function properly on Internet Explorer 10 and 11 if the document mode is

When using Internet Explorer 10 or 11, rotation works perfectly fine if we select edge mode, as shown in the image. This information was taken from the High Chart Demo Charts. However, if we select Document mode 8 in IE 10 or IE 11, the rotation does not ...

The C++ private implementation (pimpl) pattern and data structure exporting concept

I have successfully implemented the pimpl pattern to conceal the implementation in my dll from external entities. But now I am facing a challenge regarding how to hide a struct that needs to be exported out of the dll. Below is a simplified representation ...

The Materialize tooltip functionality is only functional when used outside of a function

I'm experiencing difficulties in manipulating this particular HTML element: <a id="q" class="btn tooltipped" data-position="bottom" data-delay="50" data-tooltip="I am a tooltip">Hover me!</a> using this specific JavaScript code: $(&apos ...

The svh/lvh units are experiencing unexpected issues when using Chrome on an iPhone

I'm facing an issue with my hero section that is supposed to take up the full height of the viewport. I recently tried using the new svh/lvh units, which seemed to work fine on desktop and Safari for mobile. However, when testing on Chrome for mobile ...

The process of updating a session using jQuery

Whenever a checkbox is selected, the session gets populated with an id. However, if another checkbox is selected afterwards, the session remains unchanged. This is my current code structure: <input type='checkbox' class='imei' id=&a ...

List created using PHP

After successfully generating a list using PHP with elements selected from a database, I encountered an issue. It seems that when trying to retrieve the index of li elements using jQuery, the indices returned are only odd numbers. For example, clicking on ...

Bone structure template - optimizing list spacing with CSS

Currently, I am attempting to float my navigation bar further towards the left by at least 200px, closer to the end of the line visible below. However, every time I add a margin or padding, it causes the elements within the navigation bar to stack on top o ...

Execute Django code post webpage loading

Looking for a way to have the python code on the web page update every second if the database has changed, but currently it only runs on page load due to django functionality. function refresh(){ //Clear info boxes {% for TrackedObject in ...

I have a desire to use Javascript to control CSS styles

Within the CSS code below, I am seeking to vary the size of a square randomly. This can be achieved using the Math.random() property in Javascript, but I am uncertain how to apply it to define the size in CSS. #square { width: 100px; height: ...

What is the best way to place a floating elastic div on top of an elastic background and ensure that the div stays in place?

Looking for some creative input here. Don't have a specific question in mind, but open to ideas. Just to clarify, I want both the floating div and the background to resize as the window size changes, while keeping the div in the same position on top ...

Utilize the display: flex property to position a div in the bottom right corner of its containing element

I'm trying to showcase a yellow div "icon" at the bottom right corner of its red container "content". Can someone help me figure out what's incorrect in my code? .root { display: inline-flex; background-color: red; } .content { d ...