0

I'm trying to load static JS and jQuery code into static html file in express, which is very new to me. I've set up the app and can access it at localhost:3000 with HTML and CSS loading as wanted, but the jQuery and JS isn't.

HTML head:

<head>
<meta charset="utf-8">
<title>Intellectual Principles</title>
<meta name="description" content="Intellectual Principles">
<meta name="author" content="Ronnlidd">
<link rel="stylesheet" type="text/css" href="/stylesheets/style.css">
<script src="../node_modules/jquery/dist/jquery.min.js"></script>
<script type="text/javascript" src="/javascripts/BoostOdds.js"></script>
<!-- COMMENTS FOR UNDERSTANDING THIS FILE -->
<!-- class="p" is default principle box, class="pv2" is a second version of a principle box, suitable for grid-items, often overwritten withs styles for specifici levels of principles.-->
</head>

Folder Structure: Open img in new tab.

App.js

    var express = require('express');
var path = require('path');
var cookieParser = require('cookie-parser');
var logger = require('morgan');

var indexRouter = require('./routes/index');
var usersRouter = require('./routes/users');

var app = express();

app.use(logger('dev'));
app.use(express.json());
app.use(express.urlencoded({ extended: false }));
app.use(cookieParser());

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


app.use('/', indexRouter);
app.use('/users', usersRouter);

module.exports = app;

Node console:

PS C:\CodingProjects\Principles> npm start

> principles@0.0.0 start C:\CodingProjects\Principles
> node ./bin/www

GET / 304 7.392 ms - -
GET /stylesheets/style.css 304 1.311 ms - -
GET /node_modules/jquery/dist/jquery.min.js 404 4.623 ms - 177
GET /javascripts/BoostOdds.js 304 5.782 ms - -

BoostOdds.js

function oneElmToggle(elm) {
    const display = elm.nextSibling.style.display;
    if (display === "none" || display === "") {
      elm.nextSibling.style.display = "block";
    }
    else {
      elm.nextSibling.style.display = "none";
    }
  }
  window.onload = function () {
    $("#toggleAllElms").click(function () {
      $(".toggleAcronym").toggle('fast');
    });
  };

Corresponding HTML (in index.html):

<div id="p1136Acronyms"> <!-- First jQuery/JS -->
    <button onclick="oneElmToggle(this)">RWE</button>
    <p class="toggleAcronym">Real World Example</p> 

    <button onclick="oneElmToggle(this)">CRUD</button>
    <p class="toggleAcronym">Create, Read, Update, Delete</p>

    <button onclick="oneElmToggle(this)">CNS</button>
    <p class="toggleAcronym">Central Nervous System</p>

    <button onclick="oneElmToggle(this)">MPS</button>
    <p class="toggleAcronym">Muscle Protein Synthesis</p>

    <button onclick="oneElmToggle(this)">I.e.</button>
    <p class="toggleAcronym">In essence</p>

    <button onclick="oneElmToggle(this)">ALAP</button>
    <p class="toggleAcronym">As Long As Possible</p>

    <button onclick="oneElmToggle(this)">AMAP</button>
    <p class="toggleAcronym">As Much As Possible</p>

    <button onclick="oneElmToggle(this)">CoC</button>
    <p class="toggleAcronym">Contents of Consciousness</p>

    <button onclick="oneElmToggle(this)">RR(P)(F)-R</button>
    <p class="toggleAcronym">Risk Reward (Probability)(Fragility) - Ratio</p>

    <button onclick="oneElmToggle(this)">AoL</button>
    <p class="toggleAcronym">Area of Life (My arbitrarily created 4 AoL are: Intellectual, Physical, Relationships & Intellectual)</p>

    <button onclick="oneElmToggle(this)">MBS</button>
    <p class="toggleAcronym">Mind Body & Spirit</p> 

    <button onclick="oneElmToggle(this)">QoC</button>
    <p class="toggleAcronym">Quality of Consciousness</p>

    <button onclick="oneElmToggle(this)">PFC</button>
    <p class="toggleAcronym">Pre-Frontal Cortex</p>

    <button onclick="oneElmToggle(this)">SRV</button>
    <p class="toggleAcronym">Survival & Reproduction Value. //(Often used to describe factors with directional effect on this.)</p>

    <button onclick="oneElmToggle(this)">P/T-R</button>
    <p class="toggleAcronym">Practice/Theory-Ratio</p>

    <button id="toggleAllElms"><h2>Toggle All</h2></button>
    <p class="toggleAcronym">This button toggles all acronym explanations to Show/Hide</p>
</div>

The strange thing to me is that the JS seems to be loading, but the functionality of the code, which I've tested to be working before, isn't loading. The jQuery seems completely lost too even though I've checked several times to see that it is really located in Principles/node_modules/jquery/dist/jquery.min.js

Thanks in advance for the help and please ask for any info I left out that you need.

David Rönnlid
  • 91
  • 1
  • 11
  • https://stackoverflow.com/a/29537014 – Smollet777 Dec 02 '18 at 10:10
  • 1
    Possible duplicate of [how to use jQuery installed with npm in Express app?](https://stackoverflow.com/questions/14264429/how-to-use-jquery-installed-with-npm-in-express-app) – jordiburgos Dec 02 '18 at 10:19
  • Thanks, I learned a lot from that, (not being sarcastic). But the problem isn't solved. I used their tips to the best of my ability, pasting `` to head of html and `app.use('/jquery', express.static(__dirname + '/node_modules/jquery/dist/')); ` to app.js. Unfortunately it's still returning the same 404 error in node console for jQuery and no functionality of JS nor jQuery is active, JS still returns 304 code in node console, which I understand as practically equivalent of 200 'OK' in this instance. Thanks for the help again. – David Rönnlid Dec 02 '18 at 10:36
  • File structure image not visible, but why not just load jQuery in the head section of the undex html file with the cdn link – ptts Dec 02 '18 at 11:59
  • I tried, didn't work with cdn link either, jQuery, that is. I found the problem with the JS though, nextSibling is the p element only if the p element is on the same line in html as button element. So, now JS works as it should, but not jQuery. – David Rönnlid Dec 02 '18 at 12:03
  • 1
    Aha! Solved it! ` ` in head of HTML & re-arrangement of HTML elements as described in above comment made JS and jQuery work as expected. Thanks for all of the help! – David Rönnlid Dec 02 '18 at 12:05

0 Answers0