2

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?

VB_
  • 45,112
  • 42
  • 145
  • 293
  • Have you looked into Browserify? It will save you some time and boilerplate. – elclanrs Nov 07 '14 at 23:37
  • @elclanrs is that an alternative to requirejs? I don't want to give up because of such stupid bug ) Hope you'll help me – VB_ Nov 07 '14 at 23:38
  • @elclanrs the problem is that express doesn't send `require.min.js` to the client. I think that's the reason why it doesn't work – VB_ Nov 07 '14 at 23:41
  • Have you tried watching the network tab in chrome dev tools or Firebug in FF when loading your page? – Ray Stantz Nov 08 '14 at 00:01
  • 3
    possible duplicate of [Why don't self-closing script tags work?](http://stackoverflow.com/questions/69913/why-dont-self-closing-script-tags-work) I'm not guessing, I replicated your environment here and with the self closing tag nothing loads but if I change it to `` then it works. – Louis Nov 08 '14 at 00:19
  • @Louis you solve my problem! Thanks!! – VB_ Nov 08 '14 at 08:59
  • @Louis get the next problem - my app can't fine `data-main` file. I loaded demo to https://github.com/JOLO-/NodeApp – VB_ Nov 08 '14 at 09:46
  • @V_B You should post a new question. The community has already responded to the question you first asked. Now you're asking a different problem. Sure it is still that your code won't load but the 1st issue was an HTML one whereas now it seems to be a RequireJS issue. It's called a ["chameleon question"](http://meta.stackexchange.com/q/43478/241526). I glanced at the off-site code and see some config differences that are pertinent to your problem. The question you post on SO must contain all the pertinent information in the *body of the question itself*. – Louis Nov 08 '14 at 11:19
  • @V_B Oh, and don't put the `.js` extension in the names of your modules when you tell RequireJS to load them. `data-main` contains the name of a module. So no extension there. I don't know whether this has any bearing on your problem. RequireJS will accept a module name with `.js` but this changes the rules it then follows for path resolution and since I *never* put `.js` in module names I don't remember how RequireJS behaves when there is a `.js` extension in a module name. – Louis Nov 08 '14 at 11:21
  • @V_B And now I see you have posted a [new question](http://stackoverflow.com/q/26816008/1906307) before I posted my comment above about doing just this. All the more reason to delete this question. – Louis Nov 08 '14 at 11:32

1 Answers1

7

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.

Community
  • 1
  • 1
Hazem Hagrass
  • 9,329
  • 10
  • 32
  • 54
  • @HazenHagrass look pls at problem 2 section of my quesiton – VB_ Nov 08 '14 at 09:48
  • @HazenHagrass it still looks for `main.app`. Look pls at https://github.com/JOLO-/NodeApp – VB_ Nov 08 '14 at 10:41
  • @V_B why you are adding this app.use('js/app.app', express.static(__dirname + '/public/js/app.js')); it is not correct, I think you should remove this line – Hazem Hagrass Nov 08 '14 at 11:08
  • I tried. That doesn't work anyway. Just accidentally pushed it – VB_ Nov 08 '14 at 11:17
  • I attached a link in my answer about Q2, I'm sure it will solve your issue. – Hazem Hagrass Nov 08 '14 at 11:27
  • run RequireJS API ref?) I've read it, but seems I'm missing smth – VB_ Nov 08 '14 at 14:05
  • you could see the answer to the second question here http://stackoverflow.com/questions/26816008/requirejs-search-for-app-app-instead-of-app-js-where-app-js-is-entry-point-spec. Modify your answer due it please – VB_ Nov 08 '14 at 14:39