为什么我的html文件没有加载express.static? [英] Why isn't my html file being loaded with express.static?

查看:126
本文介绍了为什么我的html文件没有加载express.static?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

文件结构:

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

我有:

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

我在index.html中加载的资产已正确加载.

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:

这是为什么? statement.directive.html嵌套在client文件夹中,该文件夹由express.static处理.我知道它不是直接嵌套在client的下面,但是我的其他许多资产也都没有正确加载.我看到的唯一区别是使用<script>/<link/>与HTTP请求.如果这是问题所在,除了为每个资产设置一个端点外,我不确定如何解决它.

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.

侧面问题:通过express.static提供lib文件夹是否不好?我不这么认为,因为它们都是公开可用的.我不确定在不为每个文件夹编写显式GET端点的情况下还能如何为该文件夹中的文件提供服务.

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.

我的png文件也未提供.他们在client > assets > images下.

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

推荐答案

考虑:app.use(express.static('client'));.

第一部分是使用一个参数调用 app.use .基本上,这意味着传递给它的回调参数将在对应用程序的每个请求中执行.

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.

第二部分是它传入express.static('client').基本上说:

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

当我们收到对/foo/bar.ext的请求时,我们将检查/client/foo/bar.ext是否存在.如果是这样,我们会为您服务.如果没有,我们不会.

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

所以它会看看是否

/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


关于使用express.static提供的库:


As for serving libs with express.static:

不,使用静态服务器中间件为库提供服务还不错

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

- Chev

这篇关于为什么我的html文件没有加载express.static?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆