admin管理员组

文章数量:1221399

I'm running a node.js server on heroku using the express.js framework.

Here is what my server looks like:

var express = require('express');
var app = express();

app.use(express.static(__dirname + '/static'));
var port = process.env.PORT || 8000;

app.listen(port);

My index.html file has the following javascript links:

<script src="/js/chartview.js"></script>
<script src="/js/bootstrap.js"></script>
<script src="/js/bootstrap-select.js"></script>

My directory system looks like this:

server.js
/static
    -index.html
    -/js
        -bootstrap.js
        -bootstrap-select.js
        -chartview.js
    -/css
        -bootstrap.css
        -bootstrap-select.css
        -styles.css

Chrome displays my html page with the appropriate css styles but the console says:

Failed to load resource: the server responded with a status of 404 (Not Found)   .js
Failed to load resource: the server responded with a status of 404 (Not Found)   .js
Failed to load resource: the server responded with a status of 404 (Not Found)   .js

It loads properly when I change my index.html tags to look like this:

<script src="chartview.js"></script>
<script src="bootstrap.js"></script>
<script src="bootstrap-select.js"></script>

And I change my directory systems to look like this:

server.js
/static
    -index.html
    -bootstrap.js
    -bootstrap-select.js
    -chartview.js
    -/css
        -bootstrap.css
        -bootstrap-select.css
        -styles.css

This is not Ideal, as I would like to have a /js folder.

Any suggestions will be greatly appreciated!

I'm running a node.js server on heroku using the express.js framework.

Here is what my server looks like:

var express = require('express');
var app = express();

app.use(express.static(__dirname + '/static'));
var port = process.env.PORT || 8000;

app.listen(port);

My index.html file has the following javascript links:

<script src="/js/chartview.js"></script>
<script src="/js/bootstrap.js"></script>
<script src="/js/bootstrap-select.js"></script>

My directory system looks like this:

server.js
/static
    -index.html
    -/js
        -bootstrap.js
        -bootstrap-select.js
        -chartview.js
    -/css
        -bootstrap.css
        -bootstrap-select.css
        -styles.css

Chrome displays my html page with the appropriate css styles but the console says:

Failed to load resource: the server responded with a status of 404 (Not Found)   https://websitename.herokuapp.com/js/chartview.js
Failed to load resource: the server responded with a status of 404 (Not Found)   https://websitename.herokuapp.com/js/bootstrap.js
Failed to load resource: the server responded with a status of 404 (Not Found)   https://websitename.herokuapp.com/js/bootstrap-select.js

It loads properly when I change my index.html tags to look like this:

<script src="chartview.js"></script>
<script src="bootstrap.js"></script>
<script src="bootstrap-select.js"></script>

And I change my directory systems to look like this:

server.js
/static
    -index.html
    -bootstrap.js
    -bootstrap-select.js
    -chartview.js
    -/css
        -bootstrap.css
        -bootstrap-select.css
        -styles.css

This is not Ideal, as I would like to have a /js folder.

Any suggestions will be greatly appreciated!

Share Improve this question edited Dec 13, 2014 at 4:29 atasca10 asked Dec 12, 2014 at 2:19 atasca10atasca10 371 gold badge1 silver badge8 bronze badges 4
  • Look closely at the urls, the /js is missing from https://websitename.herokuapp.com/chartview.js etc . . . – generalhenry Commented Dec 12, 2014 at 2:21
  • 1 what comes up when you go to the url manually, still 404? – generalhenry Commented Dec 12, 2014 at 2:24
  • typing websitename.herokuapp.com/js/chartview.js in chrome displays "Cannot GET /js/chartview.js" in the browser. – atasca10 Commented Dec 12, 2014 at 2:25
  • Are the directory and file permissions all the same (css, js, and html)? – mscdex Commented Dec 12, 2014 at 2:32
Add a comment  | 

3 Answers 3

Reset to default 12

From the console error, https://websitename.herokuapp.com/js/chartview.js, the file is not being checked in static folder.

So, change your index.html to include scripts like this.

<script src="js/chartview.js"></script> i.e remove / from beginning. as path is relative to index.html.

Follow same process for other three files.

Also, you mentioned that the CSS files are loaded correctly. Can you show how exactly are you including those css files? i.e the path for css.

Comment here, if that doesn't help.

Try Placing your files in the public folder and use the path like this""

Don't use "__dirname" Only using

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

本文标签: