htmlcsspathlocalhoststatic-resource

Why does node.js app CSS load in my IDE but not on a localhost?


I am making a nodejs application using LightTable, and the css seems to be loading fine when I indicate a relative path to the file in my <link href=""> tag. When I run node index.js, however, the server starts up and the page loads fine, but I get a console error and my own 404 saying the CSS file could not be found at the path specified. I tried using Heroku's Foreman Start as well, but no dice. Here is the html:

<!DOCTYPE html>
<html>
    <head>
        <title>
            Home
        </title>
        <link href="../../styles/master.css" rel="stylesheet" type="text/css" />
    </head>
...

Here is my directory tree structure:

.
├── Procfile
├── README.md
├── index.js
├── package.json
├── resources
│   └── images
│       └── tokyo.jpg
├── styles
│   └── master.css
└── views
    ├── get
    │   ├── home.html
    │   └── posts.html
    └── post
        └── new.html 

EDIT: after finding the answer to the question, the below information became relevant. I have commented out the new code that makes the CSS and background image load as desired.

Here is my routing and server code. I am not using Express, on purpose:

    // routes
    var homeREGEX = new RegExp('^/?$');
    var newREGEX = new RegExp('^/posts/new/?$');
    var postsREGEX = new RegExp('^/posts/?$');
 // var cssREGEX = new RegExp('^/styles/master.css/?$');             <--| static resources also
 // var tokyoREGEX = new RegExp('^/resources/images/tokyo.jpg/?$');  <--| need routes. 

    // server
    var server = http.createServer(function(request, response){
      var pathname = url.parse(request.url).pathname;

      if (homeREGEX.test(pathname)) {
        renderHome(request, response);

      } else if (newREGEX.test(pathname)) {
        renderPostForm(request, response);
      } else if (postsREGEX.test(pathname)) {
        addNewPost(request, response);
   // } else if (cssREGEX.test(pathname)) {           <--| and they need to get
   //   sendCSS(request, response);                   <--| served individually as
   // } else if (tokyoREGEX.test(pathname)) {         <--| they are requested
   //   sendTokyo(request, response);
      } else {
        error404(request, response);
      }
    });

Solution

  • What's the url you use in your browser when viewing the page ? My guess is that is does not contains the views/get part, which means that you don't need the ..\.. in your link href attributes.

    Also, make sure your are actually serving these static files (it seems you're not). If you're using express (with nodejs ) for example, you'll need something like this :

    app.use(express.static('resources'));
    app.use(express.static('style'));
    

    See express.static documentation.