0

I have a script, TemplateLoader.js which loads 2 Mustache templates, and renders them on the page (or at least that's the goal).

My directory structure:

COMP266
    Unit 4
        scripts
            mustache.min.js
            TemplateLoader.js
        PageUsingTemplateLoader.html

Inside of TemplateLoader (the object), I have the following chunk to load Mustache, and render the templates:

$.getScript("./scripts/mustache.min.js", function() {
    $('head').html( Mustache.render(headTemplate, data) );
    $('body').html( Mustache.render(bodyTemplate, data, uniqueBodyTemplate) );
});

This however, yields the following error in the developer console:

HTTP404: NOT FOUND - The server has not found anything matching the requested URI (Uniform Resource Identifier). (XHR): GET - http://localhost:63342/COMP266/Unit%204/mustache.min.js?_=1450903391318

Oddly, it seems to have dropped the script folder completely from the path.

I decided to play around, so I changed the fetch path to (duplicating the script folder):

./scripts/scripts/mustache.min.js

But this yields:

HTTP404: NOT FOUND - The server has not found anything matching the requested URI (Uniform Resource Identifier). (XHR): GET - http://localhost:63342/COMP266/Unit%204/scripts/scripts/mustache.min.js?_=1450903743022

Now it's listening! Unfortunately, this is obviously the wrong path.

I have no idea how to go about debugging this. It seems like jQuery is selectively dropping the scripts folder. That seems ridiculous, but just to make sure, I searched through the jQuery source, and couldn't find anything that would be doing the observed filtering of the path.

It's currently local, not hosted.

Can anyone give me a hint about what's going on here?

Carcigenicate
  • 43,494
  • 9
  • 68
  • 117
  • No idea really but I'd do the following: view the source of TemplateLoader.js as delivered to the browser. I don't know what your web server is but I suspect some configuration on the server is re-writing the path; the next thing would be to simply change the name of the scripts folder to js and see if that fixes the issue. – bknights Dec 23 '15 at 21:03
  • To me it looks like some kind of urlrewrite is interfering. – Kevin B Dec 23 '15 at 21:04
  • @bknights It's local currently, so there shouldn't be anything messing with it. – Carcigenicate Dec 23 '15 at 21:04
  • @KevinB I don't even know what that is. Is that a part of jQuery? – Carcigenicate Dec 23 '15 at 21:05
  • No, it would be server-side. think, .htaccess, or web.config. – Kevin B Dec 23 '15 at 21:05
  • @KevinB It's local currently, so it can't be that. I've updated the question. – Carcigenicate Dec 23 '15 at 21:06
  • what if you write "scripts/mustache.min.js" – Ludovic C Dec 23 '15 at 21:06
  • localhost can have .htaccess and/or web.config, so it being "local" is irrelevant, unless you're working from file:/// – Kevin B Dec 23 '15 at 21:08
  • do you really need the space in `Unit 4` folder ? Are you sure, it is not messing things up? – Alp Dec 23 '15 at 21:09
  • @ludo It looks at `http://localhost:63342/COMP266/Unit%204/mustache.min.js?_=1450905003395` – Carcigenicate Dec 23 '15 at 21:11
  • Since you are using http then there is some web server involved. I'm pretty sure IIS for instance had scripts set up as a magic folder. – bknights Dec 23 '15 at 21:11
  • @kevinB Unless WebStorm has set something up, there shouldn't be any configuration going on. I'll do a search. Thanks. – Carcigenicate Dec 23 '15 at 21:12
  • @Alp I'll trying removing the space. Edit: No change. It has the same behavior. – Carcigenicate Dec 23 '15 at 21:12
  • @bkknights This might be Webstorm doing something then. – Carcigenicate Dec 23 '15 at 21:14
  • What's your server ? Node.js ? Grunt connect? IIS ? Apache ? – Ludovic C Dec 23 '15 at 21:15
  • @Ludo I have done nothing to set up a web server. The site will eventually be hosted on my school site, but for now, I wrote up the pages/scripts in Webstorm, then asked it to run it. I'm starting to think it's doing something. – Carcigenicate Dec 23 '15 at 21:18
  • I see, it's WebStorm's internal server. Try running a `node` server. Or a `python -m SimpleHTTPServer 8080` server, and check if the error persists. – Ludovic C Dec 23 '15 at 21:20
  • Ya, it's WebStorm: http://blog.jetbrains.com/webide/2013/03/built-in-server-in-webstorm-6/. Honestly, I'm not even sure how to go about running it on another server. That's something I'll need to learn first. – Carcigenicate Dec 23 '15 at 21:24
  • If you're on OSX, go in console, `cd` to your directory where `index.html` is, and write `python -m SimpleHTTPServer 8080`. Then go to `localhost:8080` in your browser – Ludovic C Dec 23 '15 at 21:27
  • First just try changing the name of the scripts folder. – bknights Dec 23 '15 at 21:29
  • If you're on windows, you can always use MAMP https://www.mamp.info/en/ – Ludovic C Dec 23 '15 at 21:29
  • @Ludo I tried on Windows, but Python complains that I don't have a `SimpleHTTPServer` module. Is it an external module, or does it come with python? – Carcigenicate Dec 23 '15 at 21:33
  • @bknights Tried changing it to `js`. No change. – Carcigenicate Dec 23 '15 at 21:34
  • http://stackoverflow.com/questions/17351016/set-up-python-simplehttpserver-on-windows – Ludovic C Dec 23 '15 at 21:34
  • Hey, try this first $.getScript("./mustache.min.js – Ludovic C Dec 23 '15 at 21:35
  • @Ludo Thanks for the link. I got a Python server going, and got the same error (`http://localhost:8080/mustache.min.js?_=1450906896119`). For your second comment, error again (`http://localhost:63342/COMP266/Unit%204/mustache.min.js?_=1450906958237`) – Carcigenicate Dec 23 '15 at 21:43
  • that's extremely weird – Ludovic C Dec 23 '15 at 21:44
  • Maybe the space in Unit%20;4 should be removed ? – Ludovic C Dec 23 '15 at 21:44
  • @Ludo Already tried that. Alp suggested that above. I'm now using `js`. The weird thing is, if I point to `scripts`, it will attempt to load it (although the folder doesn't exist since the rename), but if I then point back to `js`, it drops that folder from the path. It's like something is dropping any folder that contain scripts. – Carcigenicate Dec 23 '15 at 21:49
  • Maybe you have weird permissions on that folder – Ludovic C Dec 23 '15 at 21:51
  • @Ludo It was read-only. I changed that, and nothing happened. – Carcigenicate Dec 23 '15 at 21:54
  • Try to re-create the folder, then create the files anew, and copy/paste the content of your js files in the new ones. Maybe it'll help. – Ludovic C Dec 23 '15 at 21:55
  • @Ludo I'll try that in a sec. I found something weird though. In the Python server console, after reloading the page, I got the following 2 lines: `127.0.0.1 - - [23/Dec/2015 14:56:11] "GET /js/mustache.min.js?_=1450907771020 HTTP/1.1" 200 - 127.0.0.1 - - [23/Dec/2015 14:56:11] code 404, message File not found 127.0.0.1 - - [23/Dec/2015 14:56:11] "GET /mustache.min.js?_=1450907771021 HTTP/1.1" 404 -` It looks like it's requesting the file twice, once with bad address, and once with the valid address. – Carcigenicate Dec 23 '15 at 21:58
  • Sometimes servers will look up multiple routes to find a file. – Ludovic C Dec 23 '15 at 21:59
  • @Ludo HOLY. Ok, I solved it. It turns out I'm a complete idiot. I don't know why this caused it exactly, but it did. I'll add an answer. – Carcigenicate Dec 23 '15 at 22:02
  • @Ludo Sorry to waste your time. I feel so stupid now. – Carcigenicate Dec 23 '15 at 22:09
  • Don't worry. You know how programming is, and that DUH at the end :-) – Ludovic C Dec 23 '15 at 22:13
  • @Ludo Very true. Thanks for your help. At the very least, I learned how to start a server using Python lol. – Carcigenicate Dec 23 '15 at 22:15
  • Hey and If you feel stupid now, means you're smarter than then ;-) – Ludovic C Dec 23 '15 at 22:16
  • @Ludo Ha! I like the way you think. That's a good twist. – Carcigenicate Dec 23 '15 at 22:16

1 Answers1

0

It turns out the issue was caused by me forgetting that my template added the mustache script (from a previous test), resulting in it being added twice.

I don't understand how this affected it though. It's not like the jQuery addition was succeeding while the template addition was causing the error, since changing the jQuery fetch path caused a direct change in the error message.

Sure enough though, when I removed the script import from the template, it worked.

I really don't understand how this caused a specific folder to be dropped from the path though.

Carcigenicate
  • 43,494
  • 9
  • 68
  • 117