资源未以快递方式加载 [英] Resources not loading in express

查看:77
本文介绍了资源未以快递方式加载的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我正在通过express服务器提供位于根目录中的以下index.html文件.

Lets say I am serving the following index.html file which is in the root directory via an express server.

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>

<h1>Hello</h1>

<script src="/public/system.js"></script>
<script src="/public/config.js"></script>
<script>System.import("app/main")</script>
</body>
</html>

system.jsconfig.js都位于公用目录文件夹中.

Both system.js and config.js are in the public directory folder.

要使index.html加载这些文件,我必须在我的app.js文件中包括以下行,以便可以在公用文件夹中搜索请求:

To get index.html to load these files I must include the following line in my app.js file so the requests can be searched in the public folder:

app.use(express.static(path.join(config.root, 'public')));

为什么会这样?为什么index.html不能正确引用文件而没有上述行,因为src到资源的路径是正确的? app/main也是如此.是否确实必须通过app.use()静态提供index.html使用的每个资源?

Why is this so? Why can't index.html correctly reference the files without the above line since the src path to the resources is correct? Same goes for app/main. Does every resource index.html uses really have to be served statically through app.use()?

推荐答案

node.js Web服务器默认不提供任何文件.因此,仅因为您有一个服务于index.html的路由,并不意味着node.js可以服务于任何其他文件. node.js与其他Web服务器不同,它们首先将自己视为文件服务进程,其次将其视为应用程序服务器. node.js可让您完全控制,默认情况下不提供任何文件.如果希望它为静态文件(例如脚本文件)提供服务,那么一行app.use(express.static(...))代码行将使它为特定目录中的所有文件提供服务.

A node.js web server does not serve ANY files by default. So, just because you have a route that serves index.html, that does not mean that any other file is served by node.js. node.js is not like other web servers that think of themselves first as file serving processes and second as app servers. node.js gives you full control and it serves no files by default. If you want it to serve a directory of static files (such as script files), then one single app.use(express.static(...)) line of code will cause it to serve all the files in a particular directory.

如果您要问为什么为index.html提供服务的同一静态路由不能为其他两个文件提供服务,则可能只是路径和文件位置未正确找到而导致的情况.由于您尚未真正为我们提供有关文件系统结构或路径结构的完整详细信息,因此我们无法提供有关应更改内容的确切详细信息.

If you're asking why the same static route that serves index.html does not serve the other two files, then that would simply be a case of the paths and file locations not lining up to be found appropriately. Since you haven't really given us the full detail on your file system structure or path structure, we can't offer exact specifics on what you should change.

如果system.js和config.js与index.html位于同一目录中,则应从其脚本标签前面删除路径.如果它们不在同一目录中,则可能需要另一个app.use(express.static(...))来涵盖其他路径/目录组合.

If system.js and config.js are in the same directory as index.html, then you should remove the path from in front of their script tags. If they are not in the same directory, then you may need another app.use(express.static(...)) that covers the other path/directory combination.

确实必须提供每个资源index.html使用的资源 通过app.use()静态地?

Does every resource index.html uses really have to be served statically through app.use()?

否,如果它与index.html位于同一位置并由同一路径引用,并且您使用的是覆盖整个目录的express.static(),则不会.

No it does not if it is in the same location as index.html and is referred to by the same path and if you're using an express.static() that covers that whole directory.

您希望node.js服务器发送的每个资源都必须由服务器中的某些路由明确覆盖.某些路由可能包含许多文件或文件的整个目录,但是您要处理的每个请求都必须包含一条路由.

Every resource you want the node.js server to send out must be specifically covered by some route in your server. Some routes may cover many files or an entire directory of files, but every request you want to work must be covered by a route.

这篇关于资源未以快递方式加载的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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