简体   繁体   中英

External files not loading into HTML

I have a problem loading external files into my index.html. My stylesheet as well as my JS files do not show up at all (Error 404 - Not Found). My directory structure is as follows.

文件结构

My index.html is:

<!DOCTYPE html>
<html ng-app="myApp">
<head>
    <title>Orders and Customers</title>
    <link rel="stylesheet" type="text/css" href="./assets/css/stylesheet.css">
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
    <script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.3.5/angular.min.js"></script>
    <script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.3.5/angular-route.min.js"></script>
    <script src="./../app/app.js" type="text/javascript"></script>
    <script src="./../app/controllers/customer/CustomerController.js"></script>
    <script src="./../app/controllers/order/OrderController.js"></script>
    <script src="./../app/services/customer/CustomerService.js"></script>
    <script src="./../app/services/order/OrderService.js"></script>
</head>
<body>
    <div ng-view></div>
</body>
</html>

This is what Chrome shows:

在此输入图像描述

My server.js is:

var express = require('express');
var path = require('path');
var bodyParser = require('body-parser');

var app = express();

app.use(bodyParser.json());

require('./server/config/mongoose.js');
require('./server/config/routes.js')(app);

app.use(express.static(path.join(__dirname, './views')));

app.listen(8000, function() {
console.log('Listening on Port 8000');
})

Any help is greatly appreciated!

Try this

in server.js

var router = express.Router();
router.use(express.static(path.resolve(__dirname, "../app")));

in index.html

<script src="/app.js" type="text/javascript"></script>
<script src="/controllers/customer/CustomerController.js"></script>
<script src="/controllers/order/OrderController.js"></script>
<script src="/services/customer/CustomerService.js"></script>
<script src="/services/order/OrderService.js"></script>

Router is middle-ware for capture * path. For example, /service/* matches all requests under service path. so I changed your code as to handle all resources under "/app" folder. (eg: /app/app.js , etc)

path.resolve changes path from relative to absolute. it means that you can add another current working directory. path.resolve(__dirname, "../app") add "/app" from current working directory ( views ). it parsed from "views/../app" to "/app"




(Reference) Path.resolve

The relative paths

<script src="./../app/app.js" type="text/javascript"></script>

etc, are pointing to the wrong directory.

Use the Network tab in your browser developer tools, to check what exact requests are going out.

If the whole app directory is public (Which, in my opinion, should not be the case), "./../" will refer to the views directory. Single . refers to the current directory, which in this case is, the one containing your index.html. Double .. refers to the directory above it, which here is views .

Rather than

<script src="./../app/app.js" type="text/javascript"></script>

Try using the full path:

<script src="/app/app.js" type="text/javascript"></script>
<script src="/app/controllers/order/OrderController.js"></script>

<link rel="stylesheet" type="text/css" href="../assets/css/stylesheet.css">

The above should solve your stylesheet link and for the rest use ../../ to go for higher levels. Example:

<script src="../../app/controllers/customers/CustomerController.js"> 

Each ../ is one level higher.

Try this

app.use(express.static(path.join(__dirname, 'views')));

and just use

<link rel="stylesheet" type="text/css" href="assets/css/stylesheet.css">

Thanks to SeongHyeon, I was able to figure out the solution to my problem. I really appreciate your guys' help. I'd like to elaborate more on this just for my own reference.

When declaring your static path in express, you don't need to reference anything relative to the file that you're trying to import from. Instead, you simply reference the file from the static path that you've declared.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM