Why isn't my html file being loaded with express.static?

烈酒焚心 提交于 2020-01-24 15:59:07

问题


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:

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.


回答1:


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



来源:https://stackoverflow.com/questions/31496288/why-isnt-my-html-file-being-loaded-with-express-static

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!