Skip to content

Instantly share code, notes, and snippets.

@keyserfaty
Created August 1, 2016 15:30
Show Gist options
  • Save keyserfaty/a7d3f84c340bb5aac07f67be5300b829 to your computer and use it in GitHub Desktop.
Save keyserfaty/a7d3f84c340bb5aac07f67be5300b829 to your computer and use it in GitHub Desktop.
Node not finding routes
  1. I had a problem with the routes I was calling from my index.html and the static routes that were handled by node. This were the links to my style.css and my bundle.js:
<head>
    <meta charset="utf-8">
    <link rel="stylesheet" href="public/style/style.css">
</head>
<body>
<div id="root"></div>
<script type="application/javascript" src="bundle.js"></script>
</body>

The problem with this routes was that they were not absolute but related to the path that preceeded them. This was the right way to do it:

<head>
    <meta charset="utf-8">
    <link rel="stylesheet" href="/public/style/style.css">
</head>
<body>
<div id="root"></div>
<script type="application/javascript" src="/bundle.js"></script>
</body>
  1. In regards to Node/express I had to set a route to the statics that would find both files, like this:
server.use('/', express.static(__dirname + '/dist'));

The first / adds a fake path before every static route. For instance: if instead of / I wrote /statics then all of my files from dist would now have an /statics right before the file path, like this: http://localhost:8080/statics/style.css when actually there is no such /statics folder containing the files.

The /dist added latter to the express.static method sets the route where express has to look for the static files.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment