我在看这个样本角应用。
在index.html
文件中,有几行代码如下
<script type="text/javascript" src="/static/angular.js"></script>
但是,如果仔细检查,项目中没有名为static
的文件夹。
这是怎么回事?角是如何定位这些基准的?
发布于 2014-12-03 10:51:00
角度与此无关。负责路径的是express
服务器。看看server/config.js
。您将看到这里提到的staticUrl: '/static'
。
现在打开server/server.js
(server.js是在应用程序中运行之前运行的脚本,所以所有的配置都在这个文件中完成),在第21行上,您将看到require('./lib/routes/static').addRoutes(app, config);
。这需要static.js
文件,它指示应用程序使用/static
(在config.js文件中提到)作为静态文件(如CSS和javascript文件)的路径。
发布于 2014-12-03 10:51:31
当浏览器处理该HTML文件时,布局引擎将向服务器发出单独的HTTP请求,以下载有关资源:
/static/angular.js
因为所有这些都是由服务器路由机制处理的,所以在客户端代码中不需要有一个名为static的文件夹。您的示例是使用Node.js快速路由,其中将/static路由映射到实际的物理路径。
下面是一段配置静态路由的代码:
https://github.com/angular-app/angular-app/blob/master/server/config.js
重要的部分是:
staticUrl: '/static', // The base url from which we serve static files (such as js, css and images)
以及/static映射到的目标文件夹:
distFolder: path.resolve(__dirname, '../client/dist'), // The folder that contains the application files (note that the files are in a different repository) - relative to this file
根据文档,dist
文件夹包含gruntfile结果,如果您查看gruntfile,就会发现所有使这成为可能的gruntfile。
https://github.com/angular-app/angular-app/blob/master/client/gruntFile.js
发布于 2014-12-03 11:09:06
这是服务器端现象。在这个文件server/lib/routes/static.js
中有一个中间件:
线路:9 app.use(config.server.staticUrl, express.static(config.server.distFolder));
它所做的是:如果任何http请求都是从config.server.staticUrl
启动的(这个应用程序的/static
是/static
),那么服务器就会尝试使用保存在config.server.distFolder
文件夹中的资源进行响应(这个文件夹是这个应用程序的client/dist )。
例如:当您向这个url /static/angular.js
请求时,中间件试图在client/dist/
中找到angular.js
文件。因为client/dist
目录由中间件映射到以/static
开头的url。
https://stackoverflow.com/questions/27269454
复制相似问题