Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
261 views
in Technique[技术] by (71.8m points)

node.js - Resources not loading in express

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>

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

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')));

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()?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.

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.

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.

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

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.

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...