简体   繁体   中英

JS script is not loading in node.js

GET /contact 200 33ms - 2.26kb
GET /stylesheets/bootstrap.css 304 2ms
GET /stylesheets/header.css 304 1ms
GET /stylesheets/style.css 304 2ms  
GET /stylesheets/contact.css 304 1ms
GET /javascripts/script.js 404 1ms

It keeps saying 404 when I try to load my javascript file. I have no idea why this is not working!

here are the lines that include the script:

extends layout
block css
  link(rel='stylesheet', href='/stylesheets/contact.css')
  script(type="text/javascript", src='/javascripts/script.js')

the css is loading fine. here is the beginning of the script file:

$(window).load(function() {
  function() {
    console.log("inside basil script");

so it is definitely not working.

Why could this be happening? I know it is very simple but I can't figure it out. I just started using node.js

PS here is my app.js file (well part of it)

// all environments
app.set('port', process.env.PORT || 3000);
app.set('views', __dirname + '/views');
app.set('view engine', 'jade');
app.use(express.favicon());
app.use(express.logger('dev'));
app.use(express.bodyParser());
app.use(express.methodOverride());
app.use(app.router);
app.use(express.static(path.join(__dirname, 'public')));

Do you use the http://www.senchalabs.org/connect/static.html middleware in your application to serve directory with clien side javascripts? (i am 90% sure that you use expressJS framework).

Looking at the http codes you showed, I belive you only added one directory to serve static files and not added javascripts one.

so you have something like

 app.use(express.static(__dirname + '/stylesheets'));

now you need to add

 app.use(express.static(__dirname + '/javascripts'));

Instead of

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

use

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

It's work for me

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