VB_
VB_

Reputation: 45682

Express doen not load javascript to the client

I want to use requireJS for client code. My file structure is:

ProjectRoot
 |-server.js
 |-public/
    |-index.html
    |-js/
       |-app.js
       |-lib/
          |-require.min.js
          |-underscore.js
          |-backbone.js
          |-raphael.js
       |-app/
          |-..

server.js:

var express = require('express');
var app = express();
app.use('/', express.static(__dirname + '/public'));
app.listen(8090);

index.html:

<!DOCTYPE html>
<html>
  <body>
  </body>
  <script src="js/lib/require.min.js" data-main="js/app.js" />
</html>

app.js:

require.config({
    paths: {
        'jquery': 'lib/jquery',
        'raphael': 'lib/raphael'
    },
    shim: {
        'lib/underscore': {
            exports: '_'
        },
        'lib/backbone': {
            deps: ["lib/underscore", "jquery"],
            exports: 'Backbone'
        }
    }
});

Problem: When I go to localhost:8090 I get only index.html without any .js file

Question: why Express does not send javascript to the client?

P.S. But when I go to http://localhost:8090/js/app.js I get my app.js

Also I launch my app with node server.js command

Problem 2: My app cannot load data-main file. Instead of requesting localhost:8090/js/app.js it asks for localhost:8090/js/app.app. Why app.app?

Upvotes: 2

Views: 2366

Answers (1)

Hazem Hagrass
Hazem Hagrass

Reputation: 9808

I think the problem is in static module, your nodejs code should be like that:

var express = require('express');
var app = express();
app.use(express.static(__dirname + '/public'));
app.listen(8090);

The issue in:

Q1: I think the issue may be from empty content model, so please try to use

 <script></script>

instead of

<script />

Q2: I think this is an issue in require.js version, read this for more info.

Upvotes: 7

Related Questions