2

File structure:

- simulated-selves
  - client
    - directives
      - Statement
        - statement.directive.html
  - lib
  - server
    - app.js
  - index.html

I have:

app.use(express.static('client'));
app.use(express.static('lib'));

My assets that I'm loading in index.html are being loaded properly.

<link rel='stylesheet' href='assets/app.css' />
<script src='angular.js'></script>
<script src='app.js'></script>
<script src='controllers/main.controller.js'></script>
<script src='directives/Statement/statement.directive.js'></script>
<script src='providers/statement.factory.js'></script>

But I'm getting this error:

enter image description here

Why is this? statement.directive.html is nested within the client folder, which is being dealt with by express.static. I know it's not nested directly underneath client, but neither are a bunch of my other assets that are being loaded properly. The only difference I see is using <script>/<link/> vs. an HTTP request. If that's the problem, I'm not sure how I'd get around it other than setting up an endpoint for each asset, which seems very excessive.

Side question: is it bad to serve the lib folder via express.static? I wouldn't think so because they're all publicly accessible. I'm not sure how else I'd be able to serve the files in that folder without writing explicit GET endpoints for each one.

EDIT: My png files aren't being served either. They're under client > assets > images.

Adam Zerner
  • 17,797
  • 15
  • 90
  • 156
  • At first glance I would assume it's a relative path issue. Try changing your references to start with a forward slash, making the reference root relative. Then write the path as though you are navigating from your site root to the file. Ex: `/client/directives/Statement/statement.directive.js`. If that works then you know you have a relative path issue. And no, it's not bad to serve libs with a static server middleware. – CatDadCode Jul 18 '15 at 23:23
  • @Chev doing what you said led to `404`s. – Adam Zerner Jul 18 '15 at 23:34
  • If you have a git repo I can clone, I'd be happy to help debug the issue. – CatDadCode Jul 19 '15 at 01:49
  • @Chev Thank you! - https://github.com/adamzerner/simulated-selves/tree/serve_static_assets – Adam Zerner Jul 19 '15 at 02:03

1 Answers1

0

Consider: app.use(express.static('client'));.

Part one is that it's a call to app.use with one argument. Which basically means that the callback argument passed in to it will be executed for every request to the app.

Part two is that it passes in express.static('client'). Which basically says:

When we get a request to /foo/bar.ext, we're going to check if /client/foo/bar.ext exists. If it does, we'll serve it. If not, we won't.

I happened to have been making the request to

/client/directives/Statement/statement.directive.html

So it'll look to see if

/client/client/directives/Statement/statement.directive.html

exists, and if so it'd serve it. But it obviously doesn't exist. I fixed the problem by making the request to

/directives/Statement/statement.directive.html

As for serving libs with express.static:

And no, it's not bad to serve libs with a static server middleware

-Chev

Community
  • 1
  • 1
Adam Zerner
  • 17,797
  • 15
  • 90
  • 156