OiO.lk Community platform!

Oio.lk is an excellent forum for developers, providing a wide range of resources, discussions, and support for those in the developer community. Join oio.lk today to connect with like-minded professionals, share insights, and stay updated on the latest trends and technologies in the development field.
  You need to log in or register to access the solved answers to this problem.
  • You have reached the maximum number of guest views allowed
  • Please register below to remove this limitation

How to link a javascript file to an html page using express and nodeJS

  • Thread starter Thread starter maudulus
  • Start date Start date
M

maudulus

Guest
My Node.js app is very simple, but I am having difficulty linking my JavaScript file to it. Normally, you would just put the script in the header. That doesn't work with Node, apparently, so I have tried to link it through sendFile and some other methods, but none have worked.

My JavaScript is simply:

Code:
var express = require('express');
var app = express();

app.get('/',function(req, res) {
    res.sendFile(__dirname + '/index.html');
    res.send()
});
app.listen(8888)

My HTML is also simple:

Code:
<html>
<head>
    <title>Charter</title>
    <script src="http://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
    <script src="http://code.highcharts.com/highcharts.js"></script>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
</head>
<body>
    <div id="container" style="width:100%; height:400px;"></div>
    <button id="button" class="autocompare" onclick="plotNewChart()">Add series</button>
    <script type="text/javascript">
         $(function () { $('#container').highcharts({ chart: { events: { addSeries: function () { var label = this.renderer.label('A series was added, about to redraw chart', 100, 120).attr({ fill: Highcharts.getOptions().colors[0], padding: 10, r: 5, zIndex: 8 }) .css({ color: '#FFFFFF' }) .add(); setTimeout(function () { label.fadeOut(); }, 1000); } } }, xAxis: { categories: ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'] }, series: [{ data: [29.9, 71.5, 106.4, 129.2, 144.0, 176.0, 135.6, 148.5, 216.4, 194.1, 95.6, 54.4] }] }); });
    </script>
</body>
</html>

What I want to do is link another file, myJS.js. I don't know how to do that without the typical <script src="myJS.js"></script>.

<p>My Node.js app is very simple, but I am having difficulty linking my JavaScript file to it. Normally, you would just put the script in the header. That doesn't work with Node, apparently, so I have tried to link it through <code>sendFile</code> and some other methods, but none have worked.</p>
<p>My JavaScript is simply:</p>
<pre><code>var express = require('express');
var app = express();

app.get('/',function(req, res) {
res.sendFile(__dirname + '/index.html');
res.send()
});
app.listen(8888)
</code></pre>
<p>My HTML is also simple:</p>
<pre><code><html>
<head>
<title>Charter</title>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="http://code.highcharts.com/highcharts.js"></script>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
</head>
<body>
<div id="container" style="width:100%; height:400px;"></div>
<button id="button" class="autocompare" onclick="plotNewChart()">Add series</button>
<script type="text/javascript">
$(function () { $('#container').highcharts({ chart: { events: { addSeries: function () { var label = this.renderer.label('A series was added, about to redraw chart', 100, 120).attr({ fill: Highcharts.getOptions().colors[0], padding: 10, r: 5, zIndex: 8 }) .css({ color: '#FFFFFF' }) .add(); setTimeout(function () { label.fadeOut(); }, 1000); } } }, xAxis: { categories: ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'] }, series: [{ data: [29.9, 71.5, 106.4, 129.2, 144.0, 176.0, 135.6, 148.5, 216.4, 194.1, 95.6, 54.4] }] }); });
</script>
</body>
</html>
</code></pre>
<p>What I want to do is link another file, <code>myJS.js</code>. I don't know how to do that without the typical <code><script src="myJS.js"></script></code>.</p>
 
Top